How to define a pointer to the template type?
A class template is not a type so you can't have pointers to them. You need to instantiate the class template in order to get a class based on the template and then you can make pointers to instances of that class.
Example:
template<class T>
struct B
{
A<T>* a;
};
Here B<int>
would hold a pointer to A<int>
. The unrelated class B<double>
would hold a pointer to an A<double>
etc.
An alternative could be to just store the pointer in a std::any
and cast when needed:
#include <any>
template <typename T>
struct A {
T t;
};
struct B {
template<class T>
void store(A<T>& o) {
a = &o;
}
template<class T>
A<T>& get() {
return *std::any_cast<A<T>*>(a);
}
std::any a;
};
int main() {
B b;
A<int> ai;
b.store(ai);
auto& aref = b.get<int>();
}