公共成員函式
<typeinfo>

std::type_info::operator==

bool operator== (const type_info& rhs) const;
bool operator== (const type_info& rhs) const noexcept;
比較型別
返回由兩個 type_info 物件標識的型別是否相同。

派生型別不被視為與其任何基類相同的型別。

引數

rhs
一個標識型別的type_info物件。

返回值

如果兩個 type_info 物件標識相同的型別,則返回 true,否則返回 false

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// comparing type_info objects
#include <iostream>   // std::cout
#include <typeinfo>   // operator typeid

struct Base {};
struct Derived : Base {};
struct Poly_Base {virtual void Member(){}};
struct Poly_Derived: Poly_Base {};

typedef int my_int_type;

int main() {
  std::cout << std::boolalpha;

  // fundamental types:
  std::cout << "int vs my_int_type: ";
  std::cout << ( typeid(int) == typeid(my_int_type) ) << '\n';

  // class types:
  std::cout << "Base vs Derived: ";
  std::cout << ( typeid(Base)==typeid(Derived) ) << '\n';

  // non-polymorphic object:
  Base* pbase = new Derived;

  std::cout << "Base vs *pbase: ";
  std::cout << ( typeid(Base)==typeid(*pbase) ) << '\n';

  // polymorphic object:
  Poly_Base* ppolybase = new Poly_Derived;

  std::cout << "Poly_Base vs *ppolybase: ";
  std::cout << ( typeid(Poly_Base)==typeid(*ppolybase) ) << '\n';

  return 0;
}

輸出
int vs my_int_type: true
Base vs Derived: false
Base vs *pbase: true
Poly_Base vs *ppolybase: false


異常安全

無異常保證:此成員函式從不丟擲異常。

另見