命名空间
变体
操作

typeid 运算符

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

查询类型信息。

用于必须知道多态对象动态类型的场合,以及用于静态类型识别。

目录

[编辑] 语法

typeid ( 类型 ) (1)
typeid ( 表达式 ) (2)

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

如果在使用 typeid 时标准库中 std::type_info 的定义不可见,则程序格式错误。

[编辑] 解释

如果 类型表达式 的类型是类类型或对类类型的引用,则该类类型不能是不完整类型

1) 引用表示 类型std::type_info 对象。如果 类型 是引用类型,则结果引用表示被引用类型的 cv-unqualified 版本的 std::type_info 对象。
2) 检查 表达式
  • 否则,typeid 不求值该表达式,它所标识的 std::type_info 对象表示该表达式的静态类型。不执行左值到右值、数组到指针或函数到指针的转换。
  • 然而,对于纯右值参数会(形式上)执行临时实体化:该参数必须在 typeid 表达式出现的上下文中可销毁。
(C++17 起)

如果 类型表达式 的类型是 cv-qualified 的,则 typeid 的结果引用表示 cv-unqualified 类型的 std::type_info 对象(即 typeid(const T) == typeid(T))。

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

  1. 在其他上下文中,求值此类 表达式 会导致未定义行为。

[编辑] 注意

当应用于多态类型的表达式时,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++ 标准。

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

[编辑] 参见

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