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

型の情報を照会します。

ポリモーフィックオブジェクトの dynamic type を知る必要がある場合や、静的型識別のために使用されます。

目次

翻訳のポイント: - 「Contents」を「目次」に翻訳 - HTMLタグ、属性、class名は一切変更せず保持 - C++関連の用語(Syntax, Explanation, Notes, Keywords, Example, Defect reports, See also)は原文のまま保持 - 数字や構造は完全に維持 - プロフェッショナルな技術文書としての正確性を確保

構文

typeid ( ) (1)
typeid ( ) (2)

typeid 式は、 lvalue式 であり、 静的記憶域期間 を持つオブジェクトを参照します。このオブジェクトは、多態型 std::type_info またはその派生型のconst修飾版です。

標準ライブラリの std::type_info の定義が typeid を使用する際に可視でない場合、プログラムは不適格となります。

説明

type または expression の型がクラス型またはクラス型への参照である場合、そのクラス型は incomplete type であってはなりません。

1) type が参照する std::type_info オブジェクトを指す。 type が参照型の場合、結果は参照先の型のCV修飾子なしバージョンを表す std::type_info オブジェクトを指す。
2) expression を検査する:
  • それ以外の場合、 typeid 式を評価せず 、識別される std::type_info オブジェクトは式の静的型を表す。lvalue-to-rvalue、array-to-pointer、function-to-pointer変換は行われない。
  • ただし、prvalue引数に対しては(形式的に) 一時オブジェクト化 が行われる: 引数は typeid 式が現れるコンテキストで破棄可能でなければならない。
(since C++17)

type または expression の型がcv修飾されている場合、 typeid の結果は、cv非修飾型を表す std::type_info オブジェクトを参照します(つまり、 typeid ( const T ) == typeid ( T ) となります)。

typeid が構築中または破棄中のオブジェクト(デストラクタ内またはコンストラクタ内、コンストラクタの initializer list または default member initializers を含む)で使用される場合、この typeid によって参照される std::type_info オブジェクトは、たとえそれが最も派生したクラスでなくても、構築中または破棄中のクラスを表します。

  1. 他の文脈では、このような expression を評価すると未定義動作が発生します。

注記

多態型の式に適用される場合、typeid式の評価には実行時のオーバーヘッド(仮想テーブルの参照)が生じる可能性があります。それ以外の場合、typeid式はコンパイル時に解決されます。

プログラムの終了時に typeid によって参照されるオブジェクトのデストラクタが実行されるかどうかは未規定です。

同じ型に対するtypeid式のすべての評価が同じ std::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 が完全な型名を返す実装の1つを使用した出力を示す例です。 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演算子によって返されるクラス
(クラス)