Do I need to initialize static inline variables to the main?

If I have the following A.hfile (header only):

#pragma once

struct A{
  static inline struct Initializer{
     Initializer(){
        std::cout << "init A" << std::endl;
     }
  } initializer;
};

Is this enough for #include "A.h"(from another header that main.cpp will include), so Initializer::Initializer()is it called before main()?

I read that standardization requires you to initialize static variables with dynamic initialization only before using it.

, (8.5, 9.4, 12.1, 12.6.1) . , , , .

#include ""?

+4
2

[basic.start.dynamic]/5:

, main . , odr- . , .

:

odr-use - odr-use ([basic.def.odr]), .


, :

#include "A.h" ( , main.cpp), Initializer::Initializer() main()?

. #include . odr-use it.

+3

, tower120 , :

, ( , . ).

http://en.cppreference.com/w/cpp/language/initialization

,

, odr- .

"odr-uses" - , ( ). , , A::Initializer::Initializer() main(), ( ) , , A::Initializer A::Initializer::Initializer() main()

#include "", "". Include .

:

public class A {
    public:
        A() {};
    private:
        int var;
}

main.cpp

#include "A.h"
int main()
{
    //Some Code
}

public class A {
    public:
        A() {};
    private:
        int var;
}
int main()
{
    //Some Code
}

#pragma once, , , .

0

Source: https://habr.com/ru/post/1690053/


All Articles