7
// 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?

3
  • When using MS Visual Studio, you'll need to use the correct __declspec when building the DLL and using the DLL for it to work. Commented Feb 25, 2016 at 15:44
  • Yes, I know thanks for that. Commented Feb 25, 2016 at 15:45
  • If the question is really about static variables across DLL boundaries then that should be mentioned in the title and more prominently in the text Commented Feb 25, 2016 at 23:10

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Dear Rob, thank you for the effort, your answer seems to be the most close to what I wanted for now, I really have no idea why you were downvoted, mb you wrote smth strange at the beginning, I did not take a look at the change history actually, or mb it is just sloppy stackoverflow readers whatever. I would appreciate it much if you could give some references to the standard and/or to some thrustworthy resources, and what kind of linkage is that, and how the standard/language qualify this kind of constructs..
I think I got down voted because it is a complex topic that is very hard to answer with yes/no. I also added clarification after I was downvoted. I don't think there is anything in the standard to cover dlls and shared binaries. The info is from a project lead (me) who has to manage a product with dozens of dlls and a lot of developers. We've made and learned from all of these mistakes. Bottom line: if it is shared, define it once somewhere, export it right, and the compiler takes care of the rest.
Yes, I also see that it is rather a header only library where everything is inline, or it is a shared library where everything is defined in translation units. I know that this is much clearer (Ohhh that c++... ))). But I was just curious is there something defining this situation, even not the situation with dll/executable, but even the situation when there is header like SomeOtherClass.hpp that is used in more than one translation unit, is it guaranteed that the variable would be only one. See the update as well.
If there are no dlls, only "regular" libraries and object files, then yes. Absolutely guaranteed to be only one.
But the confusion is still the same, why? Because actually there would be as many SomeOtherClass::callMe functions as translation units. How then it resolves the static variable, how does it qualify it. Because in case of regular namespace-declared static variables there would be as many variables as translation units. Then what is static local variable at all?
|
1

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.

1 Comment

I agree with you that it is the user who chooses what it wants and does it with the means of the language. But in this situation I would say that since the static function (SomeOtherClass::callMe) is duplicated in code. Than the static variable should be duplicated either, but it is not at least with g++. But on the other hand, it should not be duplicated because as the language says static guaranties single object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.