std::type_i...::operator!=(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 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 πŸ’…

  • true if the comparison operation holds true
  • false otherwise

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 another type_info object 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