名稱空間
變體
操作

typeid 運算子

來自 cppreference.com
< 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 運算子返回
(類) [編輯]