std::type_i...::operator!=(3)
πΈ NAME πΈ
okay so this is the std::type_i...::operator!=(3) page from the C++ Standard Libary manual β yes, the header spells it βLibaryβ, and honestly? iconic. π
std::type_info::operator==,std::type_info::operator!=-std::type_info::operator==,std::type_info::operator!=
π¦ Synopsis π¦
bestie, the formatting on this synopsis is lowkey doing main character energy, but here are the exact signatures and version tags, line breaks and all:
bool operator==( const type_info& rhs ) const; (until C++11)
bool operator==( const type_info& rhs ) const (since C++11)
noexcept; (until C++23)
constexpr bool operator==( const type_info& rhs (1) (since C++23)
) const noexcept;
bool operator!=( const type_info& rhs ) const;
(until C++11)
bool operator!=( const type_info& rhs ) const (2) (since C++11)
noexcept;
(until C++20)
so what do these actually do? Checks if the objects refer to the same types. β¨
and important detail: The != operator is synthesized from operator==. (since C++20) periodt.
π Parameters π
rhsβ another type information object to compare to. basically the other bestie in the comparison π«Ά
π Return value π
trueif the comparison operation holds truefalseotherwise
literally that simple, no cap. π
β¨ Notes β¨
the page has a feature-test macro table moment, and here are the exact values:
- Feature-test macro:
__cpp_lib_constexpr_typeinfo - Value:
202106L - Std:
(C++23) - Feature: Constexpr for
std::type_info::operator==
π Example π
the page says // Run this code, so run it bestie ππ:
#include <iostream>
#include <string>
#include <typeinfo>
#include <utility>
class person
{
public:
explicit person(std::string n) : name_(std::move(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 print_info(const person& p)
{
if (typeid(person) == typeid(p))
std::cout << p.name() << " is not an employee\n";
else 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()
{
print_info(employee{"Paul","Economics"});
print_info(person{"Kate"});
#if __cpp_lib_constexpr_typeinfo
if constexpr (typeid(employee) != typeid(person)) // C++23
std::cout << "class `employee` != class `person`\n";
#endif
}
Possible output, exactly as the page gives it:
Paul is an employee who works in Economics
Kate is not an employee
class `employee` != class `person`
π«Ά See also π«Ά
beforeβ checks whether the referred type precedes referred type of anothertype_infoobject in the implementation defined order, i.e. orders the referred types (public member function).
okay thatβs the whole page, bestie π source/version: http://cppreference.com, date: 2024.06.10, and the page is std::type_i...::operator!=(3). slay, bye π¦πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms