vendredi 7 juillet 2023

Is there a function to call the class name in a template<> without typing the name of the class yourself?

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)?

Aucun commentaire:

Enregistrer un commentaire