Search code examples
c++templatestypeid

Get the type which a template class consist of


Having -

C_Type.h

#ifndef C_TYPE_H
#define C_TYPE_H
template <class T>
class C_Type {
    public:
        T m_val;
            // implementation ... 

};

#endif

And a program -

#include <iostream>
#include <typeinfo>
#include "C_Type.h"
using namespace std ; 


int main () {
    C_Type<int> a  ; 
    cout <<typeid(a.m_val).name()<<endl;

}

I trying to extract the int which C_Type<int> consist of , the above program just gave output - i .

Edit :

Is it possible to get the type (i.e int or i) with no regards to the class members (i.e m_val) ?


Solution

  • The name returned by typeid::name is compiler specific and for some compiler it is something horrible (like i for int). Most compilers support demangling of names which leads to a nicer representation, but still is useless for programmatic use.

    Here is a list of demangling APIs for common compilers: