我正在尝试实现“ HasUtility”模板类的设计,以允许创建模板化的静态成员(实用程序)并维护所有实用程序的列表(基类)
代码如下:
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;
和主要内容:
#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
}
执行时,我得到以下结果:
All size is 1, address is 0x562942baa1f0
All size is 2, address is 0x562942baa1f0
Initializations done
All size is 0, address is 0x562942baa1f0
所有大小最后仍应为2。 看起来在“实用程序”成员初始化之后,“所有”已重新初始化! 地址是相同的,所以我认为它是同一对象。也许是从一个空的副本中复制过来的??欢迎任何帮助!
我使用gcc和C ++ 17
答案 0 :(得分:0)
您正在从其他静态对象(在这种情况下为StaticUtilityBase::all
)的构造函数中访问静态对象(在这种情况下为template <typename T> StaticUtility<T> HasUtility<T>::utility;
)。
由于跨翻译单元的不同静态对象的初始化之间没有排序保证,因此您尝试执行的操作将无法正常工作。
有关更多信息,请参见https://en.cppreference.com/w/cpp/language/siof。
一种简单的验证方法是在StaticUtilityBase
中添加第二个静态对象,该对象在构造时会打印出来:
static int printer = []()->int{std::cout << "Initing StaticUtilityBase\n"; return 0;}()
答案 1 :(得分:-1)
是因为您忘记初始化向量all
还是在某个时候覆盖了它?