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