[英]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)我正在尝试实现“HasUtility”模板类的设计,允许创建模板化静态成员(实用程序)并维护所有实用程序(基类)的列表
Here is the code:这是代码:
StaticUtilityBase.h: 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:模板实用程序.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.最后,所有大小仍应为 2。 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我使用 gcc 和 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;
).您正在从不同静态对象的构造函数(在本例中为template <typename T> StaticUtility<T> HasUtility<T>::utility;
)访问静态对象(在本例中为StaticUtilityBase::all
)。
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.有关更多信息,请参阅https://en.cppreference.com/w/cpp/language/siof 。
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;}()
验证这一点的简单方法是向StaticUtilityBase
添加第二个静态对象,该对象在构造时打印出来: static int printer = []()->int{std::cout << "Initing StaticUtilityBase\\n"; return 0;}()
static int printer = []()->int{std::cout << "Initing StaticUtilityBase\\n"; return 0;}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.