简体   繁体   中英

C++ static member reinitialized after initialization

Im am trying to implement a design of a "HasUtility" template class allowing to create templated static member (utilities) and maintening a list of all utilities (base classes)

Here is the code:

StaticUtilityBase.h:

#include <vector>
#include <iostream>

class StaticUtilityBase {
public:
    static std::vector<StaticUtilityBase*> all;
    StaticUtilityBase() {
        all.push_back(this);
        printAllState();
    }
    static void printAllState() {
        std::cout << "All size is " << all.size() << ", address is " << &all << std::endl;
    }
};

template <typename T> class StaticUtility : public StaticUtilityBase {};

template <typename T> class HasUtility  {
public:
    static StaticUtility<T> utility;
};

template <typename T> StaticUtility<T> HasUtility<T>::utility;

TemplateUtility.cpp:

#include "StaticUtilityBase.h"
std::vector<StaticUtilityBase*> StaticUtilityBase::all;

and the main:

#include <iostream>
#include "StaticUtilityBase.h"
class A {};
class B {};
int main() {
    std::cout << "Initializations done" << std::endl;
    StaticUtilityBase::printAllState();
    HasUtility<A>::utility; //Tell to the compiler that the static member is used so the constructor
    HasUtility<B>::utility; // of utility is used before main execution, during initializations
}

When executing I have the following result:

All size is 1, address is 0x562942baa1f0
All size is 2, address is 0x562942baa1f0
Initializations done
All size is 0, address is 0x562942baa1f0

All size should still be 2 at the end. Looks like "all" is reinitialized after the "utility" members initialization! The address is the same so I think it is the same object. Maybe it has been copied from an empty one?? Any help welcome!

I use gcc and C++17

You are accessing a static object (in this case StaticUtilityBase::all ) from the constructor of a different static object (in this case template <typename T> StaticUtility<T> HasUtility<T>::utility; ).

As there is no ordering guarantee between initialisation of different static objects across translations units what you are trying to do won't work.

See https://en.cppreference.com/w/cpp/language/siof for more information.

And easy way to verify this is to add a second static object to StaticUtilityBase which prints out when it gets constructed: static int printer = []()->int{std::cout << "Initing StaticUtilityBase\\n"; return 0;}() static int printer = []()->int{std::cout << "Initing StaticUtilityBase\\n"; return 0;}()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM