I am trying to get the type name of a class. Of course, I could just use typename(class)
but I need to place it in a template like this
ClassA<typeid(class).name()> a = ClassA<typeid(class).name()>(args);
This is what I tried:
template<class T>
class A
{
public:
T t; // the class
A(T t1)
{
t = t1;
}
};
class B // example class
{
public:
int i;
B(int i1)
{
i = i1;
}
};
int main()
{
B b = B(1);
A<typeid(b).name()>(b) a = A<typeid(b).name()>(b); // my issue
}
But it always gives me an error.
'A': invalid template argument for 'T', type expected.
Is there any way round this (class A
needs to be able to accept any class in one attribute)?
Is there any way around this (
class A
needs to be able to accept any class in one attribute)?
What you are looking for is the decltype
keyword. You need
A<decltype(b)> a = A<decltype(b)>{ b };
// or just
auto a = A<decltype(b)>{ b };
However, since c++17, thanks to CTAD, which allows you to write just.
A a{ b }; // no need of "A<-template-para-list->"
Side note: If you do not initialize the members in the constructor initializer list, the passed T
in A
must be default constructible. Therefore, I would recommend
template<class T>
class A
{
T t;
public:
A(T t1)
: t{ t1 } // ---> initialize in constructor initializer list
{}
};