const variables in header file and static initialization fiasco

后端 未结 4 1896
情歌与酒
情歌与酒 2021-02-02 16:42

After reading a lot of the questions regarding initialization of static variables I am still not sure how this applies to const variables at namespace level.

<
相关标签:
4条回答
  • 2021-02-02 17:05

    Your first definition places path1 in each compilation unit that includes config.h. To avoid this, don't define variables in header files. Usually you'd declare the variables in the header as extern:

    extern const std::string path1;
    extern const MyClass myclass1;
    

    and define them in a translation unit, e.g. config.cpp:

    const std::string path1 = "/xyz/abc";
    const MyClass myclass1("test");
    

    Sometimes you need a constant variable that is usable only from one translation unit. Then you can declare that variable at file scope as static.

    static const std::string path1 = "/xyz/abc";
    

    static is not deprecated any more. static and extern are sometimes implied, but I always forget where and how, so I usually specify them explicitly for all namespace-level variables.

    0 讨论(0)
  • 2021-02-02 17:10

    I tried to get the necessary information right from the C++03 Standard document. Here is what I found:

    Regarding the const static declarations:

    According to section 3.5.3 objects defined at namespace level and declared const have internal linkage by default. static also declares a namespace level object to have internal linkage so there is no need to declare an object static const.

    Also according to Annex D.2

    The use of the static keyword is deprecated when declaring objects in namespace scope (see 3.3.5).

    Regarding the static initialization fiasco:

    Since the variables are defined in a header file they are always defined before any other static objects using them.

    From section 3.6.2.1:

    Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.

    Answer 1: This means passing the variables to a static object constuctor should be fine.

    Answer 2: However a problem could occur if the variables are referenced from a non-inline constructor of a static object:

    Neither in section 3.6.2.1 nor 3.6.2.3 is it specified in which order the static objects in different compilation units are initialized if dynamic initialization is done before the first statement of main.

    Consider the following:

    // consts.h
    #include <string>
    
    const std::string string1 = "ham";
    const std::string string2 = "cheese";
    
    // myclass.h
    #include <string>
    
    class MyClass
    {
    public:
        MyClass();
        MyClass(std::string str);
        std::string Get() { return memberString; }
    private:
        std::string memberString;
    }
    
    // myclass.cpp
    #include "consts.h"
    #include "myclass.h"
    
    MyClass::MyClass() : memberString(string1) {}
    
    MyClass::MyClass(std::string str) : memberString(str) {}
    
    // main.cpp
    #include <iostream>
    #include "consts.h"
    #include "myclass.h"
    
    MyClass myObject1;
    MyClass myObject2(string2);
    
    using namespace std;
    
    int main()
    {
        cout << myObject1.Get(); // might not print "ham"
        cout << myObject2.Get(); // will always print "cheese"
    }
    

    Since myclass.cpp has its own copy of the const variables, these might not be initialized when MyClass::MyClass() is called.

    So yes, const variables defined in header files can be used in a way that is prone to the static initialization fiasco

    As far as I can see this does only apply to variables not requiring static initialization:

    From C++03 standard, section 3.6.2.1:

    Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place.

    0 讨论(0)
  • 2021-02-02 17:14

    What is referred as the static initialization fiasco is a problem when one namespace level variable depends on the value assigned to a different namespace level variable that might or not be initialized before. In your two examples there is no such dependency and there should not be any problem.

    This, on the other hand, is prone to that type of error:

    // header.h
    extern const std::string foo;
    
    // constant.cpp
    const std::string foo( "foo" );
    
    // main.cpp
    #include "header.h"
    const std::string foobar( foo+"bar" );
    int main() {
       std::cout << foobar << std::endl;
    }
    

    There is no guarantee that foo will be initialized before foobar, even if both are constant. That means that the program behavior is undefined and it could well print "foobar", "bar" or die.

    0 讨论(0)
  • 2021-02-02 17:20

    Static initialization fiasco refers to static variables that depend on each other. Merely defining some static const variables will not be a source of problems.

    0 讨论(0)
提交回复
热议问题