C ++ static variables in static class methods defined in headers

// SomeOtherClass.hpp #pragma once int someOtherCallMe(); class SomeOtherClass { public: static int callMe() { static int _instance = 7; ++_instance; return _instance; } }; // SomeOtherClass.cpp #include "SomeOtherClass.hpp" int someOtherCallMe() { return SomeOtherClass::callMe(); } // main.cpp #include "SomeOtherClass.hpp" #include <iostream> int main() { std::cout << SomeOtherClass::callMe(); std::cout << someOtherCallMe(); return 0; } 

I have three files: SomeOtherClass.hpp / cpp, main.cpp. These files lead to two binary files: a shared library (from SomeOtherClass.cpp) and executable files (from main.cpp associated with the shared library).

Does C ++ guarantee that static <any-type> _instance will be a single variable during program execution (no matter how many binary files were defined)?

Note Clarify the situation. The embarrassment that I see in this situation is that, on the one hand, SomeOtherClass::callMe is defined twice in the program, which is expected (since the function of a static member of a class is actually a regular function with internal communication, if they are defined in place, for example in this case), and this is what you can see when disassembling. Since we have two functions with static local variables in machine code. How do the language / standard fit their behavior?

+5
source share
2 answers

Yes. Static will be one value. Many other things are undefined or new to the standard. (When are they initialized, if they are global? Is the code for static initialization inside a function thread safe?) But, yes, you can only count on one.

The only clarification here is outside the standard, but it has practical value if you create a shared library (.so or .dll): you cannot statically (privately) link your C ++ class library to a shared library. Otherwise, it would make two copies if you do it in two different shared libraries. (This comment applies to everything related to the library, and not just static variables. If you do, then everything is duplicated.)

Edit: On many platforms (such as Linux and Windows), this can be used to purposefully β€œhide” your static variable. If you do not make your function / class accessible outside the dll / so (using the declspec attribute or the visibility attribute), you can ensure that your dll / has its own copy of the whole class. This method can help reduce unwanted interactions between libraries. However, in your case, it sounds as if you really want only one thing that will happen if your class has the appropriate visibility in all your libraries (only in one and other libraries associated with this library).

Change again to refer to standard

If a function with an external link is declared embedded in one translation unit, it must be declared embedded in all translation units in which it is displayed; no diagnostics required. The built-in function with external communication must have the same address in all translation units. A static local variable in an external inline function always refers to the same object .

7.1.2.4, C ++ 14

0
source

Does C ++ provide that static _instance will be the only variable during program execution (no matter how many binary files were defined)?

I do not think that this language has anything to say. He does not talk about static libraries or dynamic libraries.

Responsibility for implementation provides a mechanism to enable the definition of a single definition. The user must ensure that they use the mechanism provided by the implementation to determine one of the parameters of the static variable in the function.

+1
source

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


All Articles