// 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. Those files result in two binaries: shared library (of SomeOtherClass.cpp) and executable(of main.cpp, linked with shared library).
Does C++ guaranties that static <any-type> _instance
will be a single variable during the execution of a program (does not matter in how many binaries it was defined)?
Note
To clarify the situation. The confusion I see in this situation is that, on one hand, the SomeOtherClass::callMe
is defined in program twice, that is expected (because class static member function are actually a regular function with internal linkage, if they are defined in place, like in this case), and that is what you can see from disassembly. Since we have two functions with static local variables in machine code. How does the language/standard qualify their behaviour?
Yes there is difference between declaring a static variable as global and local. If it is local, it can be accessed only in the function where it's declared. But if it is global, all functions can access it.
A static variable should be declared with in the file where we use it shouldn't be exposed to header file.
It will be considered as a global static variable. It will limit its scope within that file in which that header file has been included. Generally, we should not use it because whoever will include that header file in any number of source code, it will create separate variable copy for each source file.
Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.
Yes. A static will be a single value. A lot of other things are not well defined or are new to the standard. (When are they initialized if they are global? Is the code for static initialization within a function thread-safe?) But, yes, you can count on there being only one.
The only clarification here is outside the standard but of practical importance if you are creating a shared library (.so or .dll): You can't statically (privately) link your C++ class library into the shared library. Otherwise, that would make two copies if you do this within two different shared libraries. (This comment applies to everything about a library, not just static variables. If you do this, then there is duplication of everything.)
Edit: On many platforms (such as Linux and Windows), this can be used to purposefully "hide" your static variable. If you don't make your function/class accessible outside the dll/so (using declspec or a visibility attribute), then you can ensure your dll/so has its own copy of the entire class. This technique can help reduce unwanted interactions between libraries. However, in your case, it sounds like you really want only one, which will be the case if your class has proper visibility throughout all of your libraries (only visible in one, and other libraries link to that library).
Edit again to refer to the standard
If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object.
7.1.2.4, C++14
Does C++ guaranties that static _instance will be a single variable during the execution of a program (does not matter in how many binaries it was defined)?
I don't think the language has anything to say about that. It does not talk about static libraries or dynamic libraries.
It's the responsibility of an implementation to provide the mechanism to make sure that it is possible to have one definition. It's up to a user to make sure that they use the mechanism provided by the implementation to have one definition of the static
variable in the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With