名前空間
変種

std::type_info::operator==, std::type_info::operator!=

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20未満)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
定数評価文脈
サポートされている操作
関係と性質の問い合わせ
型変更
(C++11)(C++11)(C++11)
型変換
(C++11)
(C++11)
(C++17)
(C++11)(C++20未満)(C++17)
 
 
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
bool operator==( const type_info& rhs ) const;
(C++11未満)
bool operator==( const type_info& rhs ) const noexcept;
(C++11以上)
bool operator!=( const type_info& rhs ) const;
(C++11未満)
bool operator!=( const type_info& rhs ) const noexcept;
(C++11以上)
(C++20未満)

オブジェクトが同じ型を参照しているか調べます。

引数

rhs - 比較する別の型情報オブジェクト

戻り値

比較結果が真であれば true、そうでなければ false

#include <iostream>
#include <typeinfo>
#include <string>
#include <utility>

class person
{
  public:

   person(std::string&& n) : _name(n) {}
   virtual const std::string& name() const{ return _name; }

  private:

    std::string _name;
};

class employee : public person
{
   public:

     employee(std::string&& n, std::string&& p) :
         person(std::move(n)), _profession(std::move(p)) {}

     const std::string& profession() const { return _profession; }

   private:

     std::string _profession;
};

void somefunc(const person& p)
{
   if(typeid(employee) == typeid(p))
   {
      std::cout << p.name() << " is an employee ";
      auto& emp = dynamic_cast<const employee&>(p);
      std::cout << "who works in " << emp.profession() << '\n';
   }
}

int main()
{
   employee paul("Paul","Economics");
   somefunc(paul);
}

出力:

Paul is an employee who works in Economics

関連項目

処理系定義の順序に従って、参照先の型が別の type_info の参照先の型より前かどうか、確認します。 つまり、参照先の型を順序付けます
(パブリックメンバ関数) [edit]