简体   繁体   中英

Initialization of inline static data

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM