• typeinfo类型支持库学习


    type_info是保有一个类型的实现指定信息的类,这里的类型不仅包括类,结构体,数值类型比如,int,double,还包括对象,指针。通过typeid运算符返回的类,可以获取该类型的指定信息包括类型的名称和比较二个类型相等的方法或相对顺序,hash_code等。

    成员函数

    (析构函数)

    [虚]

    虚析构函数使 type_info 为多态类
    (虚公开成员函数)

    operator==operator!=

    (C++20 中移除)

    检查对象是否指代相同类型
    (公开成员函数)

    before

    检查在实现定义的顺序中,被指代类型是否在另一个 type_info 对象之前,即对被指代类型排序
    (公开成员函数)

    hash_code

    (C++11)

    返回对于同一类型相同的值
    (公开成员函数)

    name

    类型的实现定义名称
    (公开成员函数)

    下面是具体的示例

    1. #include
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. class Person {
    7. public:
    8. Person(string name):
    9. m_name(std::move(name))
    10. {
    11. }
    12. virtual const string& name() const
    13. {
    14. return m_name;
    15. }
    16. private:
    17. string m_name;
    18. };
    19. class Employee: public Person {
    20. public:
    21. Employee(string name, string per):
    22. Person(std::move(name)),
    23. m_profession(std::move(per))
    24. {
    25. }
    26. const string &profession() const
    27. {
    28. return m_profession;
    29. }
    30. private:
    31. string m_profession;
    32. };
    33. void somefunc(const Person& per)
    34. {
    35. if(typeid(Person) == typeid(per))
    36. {
    37. cout << per.name() << " is not an employee \n";
    38. }
    39. else if(typeid (Employee) == typeid(per))
    40. {
    41. cout << per.name() << " is an Employee ";
    42. auto &emp = dynamic_cast<const Employee&>(per);
    43. cout << "who works in " << emp.profession() << "\n";
    44. }
    45. }
    46. //结构体
    47. struct SBase {
    48. public:
    49. virtual ~SBase() = default;
    50. };
    51. struct SDerived: SBase {
    52. };
    53. //类
    54. class CBase {
    55. public:
    56. virtual ~CBase() = default;
    57. };
    58. class CDerived: public CBase {
    59. };
    60. int main()
    61. {
    62. //1.1获取对象的类型信息
    63. SBase sbase;
    64. SDerived sderived;
    65. const SBase *pb = &sbase;
    66. cout << "sbase=== name = " << typeid(sbase).name() << " raw_name = " << typeid(sbase).raw_name() << " hash_code = " << typeid(sbase).hash_code() << endl;
    67. cout << "pb=== name = " << typeid(pb).name() << " raw_name = " << typeid(pb).raw_name() << " hash_code = " << typeid(pb).hash_code() << endl;
    68. cout << "*pb=== name = " << typeid(*pb).name() << " raw_name = " << typeid(*pb).raw_name() << " hash_code = " << typeid(*pb).hash_code() << endl;
    69. pb = &sderived;
    70. cout << "*pb=== name = " << typeid(*pb).name() << " raw_name = " << typeid(*pb).raw_name() << " hash_code = " << typeid(*pb).hash_code() << endl;
    71. CBase cbase;
    72. CDerived cderived;
    73. const CBase *pbc = &cbase;
    74. cout << "*pbc=== name = " << typeid(*pbc).name() << " raw_name = " << typeid(*pbc).raw_name() << " hash_code = " << typeid(*pbc).hash_code() << endl;
    75. pbc = &cderived;
    76. cout << "*pbc== name = " << typeid(*pbc).name() << " raw_name = " << typeid(*pbc).raw_name() << " hash_code = " << typeid(*pbc).hash_code() << endl;
    77. //1.2 获取普通变量的类型信息
    78. int n = 30;
    79. const type_info &info = typeid(n);
    80. cout << "int=== name = " << info.name() << " raw_name = " << info.raw_name() << " hash_code = " << info.hash_code() << endl;
    81. //1.3 获取一个字面量的类型信息
    82. const type_info &literalInfo = typeid(19.6);
    83. cout << "19.6== name = " << literalInfo.name() << " raw_name = " << literalInfo.raw_name() << " hash_code = " << literalInfo.hash_code() << endl;
    84. //1.4 获取表达式的类型信息
    85. const type_info &expressionInfo = typeid(50 / 2 * 3.6);
    86. cout << "expression name = " << expressionInfo.name() << " raw_name = " << expressionInfo.raw_name() << " hash_code = " << expressionInfo.hash_code() << endl;
    87. //1.5 获取结构体的类型信息
    88. const type_info &sinfo = typeid(SBase);
    89. cout << "SBase name = " << sinfo.name() << " raw_name = " << sinfo.raw_name() << " hash_code = " << sinfo.hash_code() << endl;
    90. //1.6 获取类的类型信息
    91. const type_info &cinfo = typeid(CBase);
    92. cout << "CBase name = " << cinfo.name() << " raw_name = " << cinfo.raw_name() << " hash_code = " << cinfo.hash_code() << endl;
    93. //2. operator==的使用
    94. somefunc(Employee{"Scott", "Camel"});
    95. somefunc(Person{"Yak"});
    96. //3. operator!=的使用
    97. if (typeid(Employee) != typeid(Person))
    98. {
    99. cout << "class 'Employee' != class 'Person' \n";
    100. }
    101. cout << endl;
    102. //4.before
    103. if(typeid(char).before(typeid(bool)))
    104. std::cout << "char goes before bool in this implementation.\n";
    105. else
    106. std::cout << "bool goes before char in this implementation.\n";
    107. if(typeid(char).before(typeid(short)))
    108. std::cout << "char goes before short in this implementation.\n";
    109. else
    110. std::cout << "short goes before char in this implementation.\n";
    111. if(typeid(int).before(typeid(char)))
    112. std::cout << "int goes before char in this implementation.\n";
    113. else
    114. std::cout << "char goes before int in this implementation.\n";
    115. cout << "Hello World!" << endl;
    116. return 0;
    117. }

    运行结果:

    接下来看一下hash_code()函数的使用:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. using namespace std;
    8. struct A {
    9. virtual ~A() {}
    10. };
    11. struct B : A {};
    12. struct C : A {};
    13. using TypeInfoRef = std::reference_wrapper<const std::type_info>;
    14. struct Hasher {
    15. std::size_t operator()(TypeInfoRef code) const
    16. {
    17. return code.get().hash_code();
    18. }
    19. };
    20. struct EqualTo {
    21. bool operator()(TypeInfoRef lhs, TypeInfoRef rhs) const
    22. {
    23. return lhs.get() == rhs.get();
    24. }
    25. };
    26. int main()
    27. {
    28. std::unordered_map type_names;
    29. type_names[typeid(int)] = "int";
    30. type_names[typeid(double)] = "double";
    31. type_names[typeid(A)] = "A";
    32. type_names[typeid(B)] = "B";
    33. type_names[typeid(C)] = "C";
    34. int i;
    35. double d;
    36. A a;
    37. // note that we're storing pointer to type A
    38. std::unique_ptr b(new B);

    运行结果:

    参考:

    - C++ Reference

    标准库头文件 - cppreference.com

  • 相关阅读:
    详解SSL证书系列(1)什么是SSL证书?
    python:激光点云生成BEV图像
    Spring源码解析之八finishBeanFactoryInitialization方法即初始化单例bean
    MyBatis关联映射例题
    测试之CSDN AI生成的GIT博文
    elementui table 合并列
    xLua背包实践
    LeetCode 算法:两两交换链表中的节点 c++
    几分钟就搞定网站速度慢、网站卡等问题
    C# 字节数组转结构体
  • 原文地址:https://blog.csdn.net/chenyijun/article/details/125822390