C++之类型萃取

C++之类型萃取

在C++中想要获得某个变量的类型有两种方式:
一种是利用typeid。
typeid是C++的关键字之一,等同于sizeof这类的操作符。typeid操作符的返回结果是名为type_info的标准库类型的对象的引用。
用法:typeid().name()

#include <iostream>
using namespace std;

class Base {};
class Derived: public Base {};

int main()
{
    Base b, *pb;
    pb = NULL;
    Derived d;

    cout << typeid(int).name() << endl
         << typeid(unsigned).name() << endl
         << typeid(long).name() << endl
         << typeid(unsigned long).name() << endl
         << typeid(char).name() << endl
         << typeid(unsigned char).name() << endl
         << typeid(float).name() << endl
         << typeid(double).name() << endl
         << typeid(string).name() << endl
         << typeid(Base).name() << endl
         << typeid(b).name()<<endl
         << typeid(pb).name()<<endl
         << typeid(Derived).name() << endl
         << typeid(d).name()<<endl
         << typeid(type_info).name() << endl;
         
    return 0;
}

另一种是利用类模板的特化,让编译器根据所传入变量的类型在调用对于特化类的函数时自动选择与之对应的类型

struct _TrueType{
    static bool Get()
    {
        return true;
    }
};
struct _FalseType{
    static bool Get()
    {
        return false;
    }
};
template <typename T>
struct TypeCheck{
    typedef _FalseType IsPODType;
};
template<>
struct TypeCheck<int>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<char>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<float>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<short>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<long>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<long long>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<unsigned int>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<unsigned char>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<double>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<unsigned short>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<unsigned long>
{
    typedef _TrueType IsPODType;
};
template<>
struct TypeCheck<unsigned long long>
{
    typedef _TrueType IsPODType;
};

template< typename TE>
bool IsPodType(TE t)
{
    return TypeCheck<TE>::IsPODType::Get();
}
int main()
{
    long l = -1;
    unsigned long ul = 0;
    float f = 1.1;
    double d = 1.1;
    string s = "1";
    char a[] = "1";
    cout << IsPodType(1)<<endl;
    cout << IsPodType("1")<<endl;
    cout << IsPodType('1') <<endl;
    cout << IsPodType(l) <<endl;
    cout << IsPodType(ul) <<endl;
    cout << IsPodType(f) <<endl;
    cout << IsPodType(d) <<endl;
    cout << IsPodType(s) <<endl; 
    cout << IsPodType(a) <<endl;
    return 0;
}

运行结果:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值