命名空间
变体
操作

typeid 运算符

来自 cppreference.cn
< cpp‎ | language
 
 
C++ 语言
 
 

查询类型的相关信息。

在必须得知多态对象动态类型,以及用于静态类型标识时使用。

目录

[编辑] 语法

typeid ( type ) (1)
typeid ( expression ) (2)

typeid 表达式是左值表达式,它引用具有静态存储期的对象,该对象是多态类型 std::type_info 或其派生类型的 const 限定版本。

如果使用 typeid 时,std::type_info 的标准库定义不可见,则程序是非良构的。

[编辑] 解释

如果 typeexpression 的类型是类类型或类类型的引用,则该类类型不能是不完整类型

1) 引用代表 typestd::type_info 对象。如果 type 是引用类型,则结果引用代表被引用类型的 cv 无限定版本的 std::type_info 对象。
2) 检查 expression
  • 否则,typeid 不对表达式求值,并且它标识的 std::type_info 对象代表表达式的静态类型。不执行左值到右值、数组到指针或函数到指针的转换。
  • 然而,对于纯右值参数,会(形式上)执行临时量实质化:实参必须在 typeid 表达式出现的语境中可析构。
(自 C++17)

如果 typeexpression 的类型是 cv 限定的,则 typeid 的结果引用代表 cv 无限定类型的 std::type_info 对象(即 typeid(const T) == typeid(T))。

如果在构造或析构期间(在析构函数或构造函数中,包括构造函数的初始化器列表默认成员初始化器)对对象使用 typeid,则此 typeid 引用的 std::type_info 对象代表正在构造或析构的类,即使它不是最派生类。

  1. 在其他语境中,对此类 expression 求值会导致未定义行为。

[编辑] 注解

当应用于多态类型的表达式时,typeid 表达式的求值可能涉及运行时开销(虚函数表查找),否则 typeid 表达式在编译时解析。

未指明是否在程序结束时执行 typeid 引用的对象的析构函数。

不保证对同一类型的所有 typeid 表达式求值都将引用相同的 std::type_info 对象,尽管它们将比较相等,但这些 type_info 对象的 std::type_info::hash_code 将是相同的,它们的 std::type_index 也将是相同的。

const std::type_info& ti1 = typeid(A);
const std::type_info& ti2 = typeid(A);
 
assert(&ti1 == &ti2); // not guaranteed
assert(ti1 == ti2); // guaranteed
assert(ti1.hash_code() == ti2.hash_code()); // guaranteed
assert(std::type_index(ti1) == std::type_index(ti2)); // guaranteed

[编辑] 关键词

typeid

[编辑] 示例

示例展示了使用 type_info::name 返回完整类型名称的实现之一的输出;如果使用 gcc 或类似工具,请通过 c++filt -t 过滤。

#include <iostream>
#include <string>
#include <typeinfo>
 
struct Base {}; // non-polymorphic
struct Derived : Base {};
 
struct Base2 { virtual void foo() {} }; // polymorphic
struct Derived2 : Base2 {};
 
int main()
{
    int myint = 50;
    std::string mystr = "string";
    double *mydoubleptr = nullptr;
 
    std::cout << "myint has type: " << typeid(myint).name() << '\n'
              << "mystr has type: " << typeid(mystr).name() << '\n'
              << "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n';
 
    // std::cout << myint is a glvalue expression of polymorphic type; it is evaluated
    const std::type_info& r1 = typeid(std::cout << myint); // side-effect: prints 50
    std::cout << '\n' << "std::cout<<myint has type : " << r1.name() << '\n';
 
    // std::printf() is not a glvalue expression of polymorphic type; NOT evaluated
    const std::type_info& r2 = typeid(std::printf("%d\n", myint));
    std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n';
 
    // Non-polymorphic lvalue is a static type
    Derived d1;
    Base& b1 = d1;
    std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n';
 
    Derived2 d2;
    Base2& b2 = d2;
    std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n';
 
    try
    {
        // dereferencing a null pointer: okay for a non-polymorphic expression
        std::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n'; 
        // dereferencing a null pointer: not okay for a polymorphic lvalue
        Derived2* bad_ptr = nullptr;
        std::cout << "bad_ptr points to... ";
        std::cout << typeid(*bad_ptr).name() << '\n';
    }
    catch (const std::bad_typeid& e)
    {
        std::cout << " caught " << e.what() << '\n';
    }
}

可能的输出

======== output from Clang ========
myint has type: i
mystr has type: NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
mydoubleptr has type: Pd
50
std::cout<<myint has type : NSt3__113basic_ostreamIcNS_11char_traitsIcEEEE
printf("%d\n",myint) has type : i
reference to non-polymorphic base: 4Base
reference to polymorphic base: 8Derived2
mydoubleptr points to d
bad_ptr points to...  caught std::bad_typeid
 
======== output from MSVC ========
myint has type: int
mystr has type: class std::basic_string<char,struct std::char_traits<char>,⮠
class std::allocator<char> >
mydoubleptr has type: double * __ptr64
50
std::cout<<myint has type : class std::basic_ostream<char,struct std::char_traits<char> >
printf("%d\n",myint) has type : int
reference to non-polymorphic base: struct Base
reference to polymorphic base: struct Derived2
mydoubleptr points to double
bad_ptr points to...  caught Attempted a typeid of nullptr pointer!

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
CWG 492 C++98 typeid 应用于 cv 限定的引用时
类型,结果代表被引用类型
结果代表
cv 无限定的被引用类型
CWG 1416 C++98 关于顶层的措辞
cv 限定可能被误解
改进了措辞
CWG 1431 C++98 typeid 仅被允许抛出 std::bad_typeid 允许抛出
可匹配的派生类
CWG 1954 C++98 不清楚是否可以检查空指针解引用
expression 的子表达式中
仅在顶层检查

[编辑] 参见

包含一些类型的信息,typeid 运算符返回的类
(类) [编辑]