Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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); but it always gives me an error. Is there any way round this (class A needs to be able to accept any class in one attribute)?

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 I always get "’A’: invalid template argument for ‘T’, type expected".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 , 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
    {}
};

Live demo in godbolt.org

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading