1

I am wondering how and when static inline data is initialized in C++ (for example in gcc or clang). I know it is a question specific to some architecture and it is not related to the C++ standard.

I know that for static/global non-inline data gcc is using the .init and .fini sections for constructing and destructing objects with such qualifiers but this won't work for inline static data because it can be initialized in many translation unit so the init section will end up with many initializations to do for a single object which is wrong.

Does anyone know how the implementation works?

To be clear I am interested in the implementation of the initialization of those inline variables.

Acorn
  • 24,970
  • 5
  • 40
  • 69
roy cabouly
  • 487
  • 4
  • 12
  • "Has anyone checked it" trust me, the standards committee (and others) have been over that many, many times. The standard defines the rules *exactly*. All you need to do is "read it". – Jesper Juhl Feb 02 '20 at 20:17
  • 3
    @JesperJuhl OP is asking about the implementation in GCC/Clang (presumably on Linux/x86-64), not the standard. – walnut Feb 02 '20 at 21:10
  • @walnut yes I want to know how inline variables are implemented in gcc/clang. I know what the standard tells about them – roy cabouly Feb 02 '20 at 21:19
  • @walnut I think that the implementation is based on weak symbols so there will be a definition in each object file and the linker chooses only one symbol.what I don't understand is how this variable is initialized which is much more complicated because only one instance needs to be initialized – roy cabouly Feb 02 '20 at 22:27

1 Answers1

0

The implementation is equivalent to that for function-local statics, which also “risk” being initialized repeatedly: an additional guard variable is created and checked to make initialization happen just once. The assembly for this process is pleasingly straightforward.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76