typeindex类型支持库学习

typeindex是针对 type_info 对象的包装,它能用作关联容器和无序关联容器的索引。下面来看它的使用示例。

#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>
#include <memory>


using namespace std;

struct SA {
    virtual ~SA() {}
};

struct SB : SA {};
struct SC : SA {};

struct CA {
    virtual ~CA() {}
};

class CB : CA {};
class CC : CA {};

int main()
{
    std::unordered_map<std::type_index, std::string> type_names;

    type_names[std::type_index(typeid(int))] = "int";
    type_names[std::type_index(typeid(double))] = "double";
    type_names[std::type_index(typeid(SA))] = "A";
    type_names[std::type_index(typeid(SB))] = "B";
    type_names[std::type_index(typeid(SC))] = "C";

    int i;
    double d;
    SA a;

    // 注意我们正在存储指向类型 A 的指针
    std::unique_ptr<SA> b(new SB);
    std::unique_ptr<SA> c(new SC);

    std::cout << "i is " << type_names[std::type_index(typeid(i))] << '\n';
    std::cout << "d is " << type_names[std::type_index(typeid(d))] << '\n';
    std::cout << "a is " << type_names[std::type_index(typeid(a))] << '\n';
    std::cout << "b is " << type_names[std::type_index(typeid(*b))] << '\n';
    std::cout << "c is " << type_names[std::type_index(typeid(*c))] << '\n';

    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

接下来我们对比一下type_info和type_info获取普通变量,字面量,表达式,结构体,类的类型信息

#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>
#include <memory>


using namespace std;

struct SA {
    virtual ~SA() {}
};

struct SB : SA {};
struct SC : SA {};

struct CA {
    virtual ~CA() {}
};

class CB : CA {};
class CC : CA {};

int main()
{
    std::unordered_map<std::type_index, std::string> type_names;

    type_names[std::type_index(typeid(int))] = "int";
    type_names[std::type_index(typeid(double))] = "double";
    type_names[std::type_index(typeid(SA))] = "A";
    type_names[std::type_index(typeid(SB))] = "B";
    type_names[std::type_index(typeid(SC))] = "C";

    int i;
    double d;
    SA a;

    // 注意我们正在存储指向类型 A 的指针
    std::unique_ptr<SA> b(new SB);
    std::unique_ptr<SA> c(new SC);


    //1 获取普通变量的类型信息
    int n = 30;
    const type_info &info = typeid(n);
    cout << "type_info   name = " << info.name() << " hash_code = " << info.hash_code() << " std::hash() = " << endl;
    const type_index &index = typeid(n);
    cout << "type_index  name = " << index.name() << " hash_code = " << index.hash_code() << " std::hash() = " << std::hash<std::type_index>{}(index) << endl;

    //2 获取一个字面量的类型信息
    const type_info &literalInfo = typeid(19.6);
    cout << "type_info   name = " << literalInfo.name() << " hash_code = " << literalInfo.hash_code() << endl;
    const type_index &literalIndex = typeid(19.6);
    cout << "type_index  name = " << literalIndex.name() << " hash_code = " << literalIndex.hash_code() << endl;

    //3 获取表达式的类型信息
    const type_info &expressionInfo = typeid(50 / 2 * 3.6);
    cout << "type_info   name = " << expressionInfo.name() << " hash_code = " << expressionInfo.hash_code() << endl;
    const type_index &expressionIndex = typeid(50 / 2 * 3.6);
    cout << "type_index  name = " << expressionIndex.name() << " hash_code = " << expressionIndex.hash_code() << endl;


    //4 获取结构体的类型信息
    const type_info &sinfo = typeid(SA);
    cout << "type_info   name = " << sinfo.name() << " hash_code = " << sinfo.hash_code() << endl;
    const type_index &sindex = typeid(SA);
    cout << "type_index  name = " << sindex.name() << " hash_code = " << sindex.hash_code() << endl;

    //5 获取类的类型信息
    const type_info &cinfo = typeid(CA);
    cout << "type_info   name = " << cinfo.name() << " hash_code = " << cinfo.hash_code() << endl;
    const type_index &cindex = typeid(CA);
    cout << "type_index  name = " << cindex.name() << " hash_code = " << cindex.hash_code() << endl;


    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

 type_index有一个hash_code()函数可以获取hash值,但同时标准库也有一个std::hash函数获取hash值,下面来对一下两个是否是一样的。type_index也提供=操作符,比较操作符,以下是示例代码:

#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>
#include <memory>


using namespace std;


int main()
{
    float f = 36.9;
    const type_index &findex = typeid(f);
    cout << "type_index  name = " << findex.name() << " hash_code =   " << findex.hash_code() << endl;
    cout << "type_index  name = " << findex.name() << " std::hash() = " << std::hash<std::type_index>{}(findex) << endl;

    float f2 = 56.9;
    const type_index &f2index = typeid(f2);

    float f3 = 36.9;
    const type_index &f3index = typeid(f3);

    if(f2 == f)
        cout << "f name = " << findex.name() << " f2 name = " << f2index.name() << " f2 == f" << endl;
    else if(f2 > f)
        cout << "f name = " << findex.name() << " f2 name = " << f2index.name() << " f2 > f" << endl;
    else if(f > f2)
        cout << "f name = " << findex.name() << " f2 name = " << f2index.name() << " f2 < f" << endl;

    if(f3 == f)
        cout << "f name = " << findex.name() << " f3 name = " << f3index.name() << " f == f3" << endl;
    else
        cout << "f name = " << findex.name() << " f3 name = " << f3index.name() << " f != f3" << endl;

    cout << "Hello World!" << endl;
    return 0;
}

运行结果:

参考:

<typeindex> - C++ Reference

标准库头文件 <typeindex> - cppreference.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值