Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

class

static

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

like image 630
user14416 Avatar asked Feb 25 '16 15:02

user14416


People also ask

Can static variable be defined in the header file?

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.

Should static variables be in header?

A static variable should be declared with in the file where we use it shouldn't be exposed to header file.

What happens if u define static member variable in 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.

Can a variable be defined in a header 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.


Video Answer


2 Answers

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

like image 138
Rob L Avatar answered Oct 06 '22 00:10

Rob L


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.

like image 43
R Sahu Avatar answered Oct 05 '22 23:10

R Sahu