Namespaces
Variants

typeid operator

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

查询某个类型的信息。

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

目录

语法

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

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

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

说明

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

1) 指代表示 type std::type_info 对象。若 type 为引用类型,则结果指向表示被引用类型经cv限定符移除后的 std::type_info 对象。
2) 检查 expression :
  • 但对于纯右值实参,(形式上)会执行 临时物化 :该实参在 typeid 表达式出现的上下文中必须是可析构的。
(C++17 起)

如果 type expression 的类型带有 cv 限定符,则 typeid 的结果指向表示无 cv 限定类型的 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); // 不保证成立
assert(ti1 == ti2); // 保证成立
assert(ti1.hash_code() == ti2.hash_code()); // 保证成立
assert(std::type_index(ti1) == std::type_index(ti2)); // 保证成立

关键词

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';
    }
}

可能的输出:

======== 来自 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
======== 来自 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 限定类型的引用时,
结果表示被引用的类型
结果表示
无 cv 限定的被引用类型
CWG 1416 C++98 关于顶层 cv 限定的措辞
可能被误解
改进了措辞
CWG 1431 C++98 typeid 仅允许抛出 std::bad_typeid 允许抛出
可匹配的派生类
CWG 1954 C++98 未明确是否可以在 表达式 的子表达式中
检查空指针解引用
仅在顶层进行检查

参见

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