Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | types‎ | type info
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
std::type_info
Member functions
type_info::operator==type_info::operator!=
(until C++20)
 
bool operator==( const type_info& rhs ) const;
(until C++11)
bool operator==( const type_info& rhs ) const noexcept;
(since C++11)
bool operator!=( const type_info& rhs ) const;
(until C++11)
bool operator!=( const type_info& rhs ) const noexcept;
(since C++11)
(until C++20)

Checks if the objects refer to the same types.

Contents

[edit] Parameters

rhs - another type information object to compare to

[edit] Return value

true if the comparison operation holds true, false otherwise

[edit] Example

#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);
}

Output:

Paul is an employee who works in Economics

[edit] See also

checks whether the referred type precedes referred type of another type_info
object in the implementation defined order, i.e. orders the referred types
(public member function) [edit]