Is there a standard analogue of the gcc construction:

__attribute__ ((constructor)) 

I understand that you can always write:

  static bool ini_once = ( do_somthing(), true ); 

, but I would like to minimize the work when porting code from gcc to the standard C ++ 11 / C ++ 14 and not reinvent unnecessary variables.

    1 answer 1

    This attribute primarily serves to bring dynamic initialization (in the C ++ style) into languages ​​in which dynamic initialization is not supported, that is, in C.

    In C ++, any dynamic initialization is an analogue. You yourself and gave an abstract example. It is possible so, but you can put these actions in the constructor of a global object.

    • The solution with the creation of an additional variable (and even more so a class), I do not like because of the clogging of the namespace. __attribute__((constructor)) allows you to perform initialization anonymously, which is what I want .. - Chorkov
    • @Chorkov Use lambdas - vegorov pm
    • @Chorkov namespace {auto unused = [] () {std :: cout << "hello world" << std :: endl; return 0;} (); } - vegorov
    • @vegorov So the global namespace is still clogged, because outside the nameless namespace, EMNIP, is automatically set using namespace …; . - HolyBlackCat 5:27 pm
    • one
      @HolyBlackCat aha, that’s why I don’t know this feature yet =) - vegorov