I try to code a simple way to execute code at start of the program automatically (without using not portable attribute()).
I wrote the following code, and I'm asking if it does violate the ODR if the code written before main() is put in header files. Or, does the inline function and variable prevent that? Would there be a case where the function would be called twice if the ODR is violated?
#include <iostream>
//-----
template <void(*init_function)()>
class execute_at_start
{
struct dummy final {};
~execute_at_start() = delete;
inline static dummy execute_() { init_function(); return dummy{}; }
inline static dummy auto_init_ = execute_();
};
//-----
inline void echo(){ std::cout << "echo" << std::endl; }
template class execute_at_start<&echo>;
int main()
{
std::cout << "EXIT SUCCESS" << std::endl;
return EXIT_SUCCESS;
}
Thank you in advance