获取模板类所包含的类型

URL*_*L87 3 c++ templates typeid

有 -

C_Type.h

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

};

#endif
Run Code Online (Sandbox Code Playgroud)

一个程序 -

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


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

}
Run Code Online (Sandbox Code Playgroud)

我试图提取int哪个C_Type<int>组成,上面的程序只给出了输出 - i.

编辑:

是否可以获得类型(即int或i)而不考虑类成员(即m_val)?

pmr*_*pmr 5

typeid :: name返回的名称是特定于编译器的,对于某些编译器来说,它是可怕的(比如ifor int).大多数编译器支持对名称进行解码,从而导致更好的表示,但仍然无法用于程序化.

以下是常见编译器的demangling API列表: