运行时类型识别
定义于头文件 |
针对 type_info 对象的包装,它能用作关联容器和无序关联容器的索引
std::type_index
class type_index; | (C++11 起) |
成员函数
(构造函数) | 构造对象 (公开成员函数) |
(析构函数) (隐式声明) | 销毁 type_index 对象(公开成员函数) |
operator= (隐式声明) | 对 type_index 对象赋值(公开成员函数) |
operator== operator!= operator< operator<= operator> operator>= | 比较底层 std::type_info 对象 (公开成员函数) |
hash_code | 返回哈希码 (公开成员函数) |
name | 返回关联到底层 type_info 对象的实现定义名称 (公开成员函数) |
辅助类
std::hash<std::type_index> (C++11) | std::type_index 的散列支持 (类模板特化) |
构造对象
std::type_index::type_index
type_index( const std::type_info& info ) noexcept; | (C++11 起) |
从 std::type_info 对象构造 type_index。
参数
info | - | type_info 对象 |
比较底层 std::type_info 对象
std::type_index::operator==,!=,<,<=,>,>=
bool operator==( const type_index& rhs ) const noexcept; | (C++11 起) | |
bool operator!=( const type_index& rhs ) const noexcept; | (C++11 起) | |
bool operator<( const type_index& rhs ) const noexcept; | (C++11 起) | |
bool operator<=( const type_index& rhs ) const noexcept; | (C++11 起) | |
bool operator>( const type_index& rhs ) const noexcept; | (C++11 起) | |
bool operator>=( const type_index& rhs ) const noexcept; | (C++11 起) |
比较底层 std::type_info 对象。
1-2) 检查底层的 std::type_info对象是否表示同一类型。
3-6) 按实现定义顺序的定义,比较底层 std::type_info 对象。比较由 type_info::before 执行。
参数
rhs | - | 要比较的另一 type_index 对象 |
返回值
1) 若底层 std::type_info 对象表示同一类型则为 true ,否则为 false 。
2) 若底层 std::type_info 对象不表示同一类型则为 true ,否则为 false 。
3-6) 若底层 std::type_info 所表示的类型按对应顺序排列,则为 true ,否则为 false 。
返回哈希码
std::type_index::hash_code
std::size_t hash_code() const; | (C++11 起) (C++14 前) | |
std::size_t hash_code() const noexcept; | (C++14 起) |
返回关联的 std::type_info 对象的 hash_code 。等价于直接调用 type_info::hash_code 。
参数
(无)
返回值
关联的 type_info 对象的 hash_code 。
返回关联到底层 type_info 对象的实现定义名称
std::type_index::name
const char* name() const; | (C++11 起) (C++14 前) | |
const char* name() const noexcept; | (C++14 起) |
返回关联的 std::type_info 对象名称。等价于直接调用 std::type_info::name() 。
参数
(无)
返回值
关联的 type_info 对象名称。
辅助类
std::type_index 的散列支持
std::hash<std::type_index>
template<> struct hash<type_index>; | (C++11 起) |
std::hash 对 std::type_index 的模板特化允许用户获得 std::type_index 类型对象的哈希。
成员 operator() 等效地返回与 hash_code() 相同的值。
调用示例
#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
#include <string>
#include <memory>
struct A
{
virtual ~A() {}
};
struct B : A {};
struct C : A {};
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(A))] = "A";
type_names[std::type_index(typeid(B))] = "B";
type_names[std::type_index(typeid(C))] = "C";
int i;
double d;
A a;
// 注意我们正在存储指向类型 A 的指针
std::unique_ptr<A> b(new B);
std::unique_ptr<A> c(new C);
std::cout << "i is " << type_names[std::type_index(typeid(i))] << std::endl;
std::cout << "d is " << type_names[std::type_index(typeid(d))] << std::endl;
std::cout << "a is " << type_names[std::type_index(typeid(a))] << std::endl;
std::cout << "b is " << type_names[std::type_index(typeid(*b))] << std::endl;
std::cout << "c is " << type_names[std::type_index(typeid(*c))] << std::endl;
return 0;
}