Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The == operator is a binary equality operator that compares two operands for value equivalence, returning a prvalue of type bool. It yields true if the operands evaluate to the same value and false otherwise. The C++ standard explicitly distinguishes equality operators (==, !=) from relational operators (<, >, <=, >=).

Syntax and Mechanics

lhs == rhs
  • Arity: Binary (requires a left-hand side and right-hand side operand).
  • Associativity: Left-to-right.
  • Precedence: Evaluated after relational operators and before the bitwise AND operator (&).

Built-in Type Semantics

For fundamental types, the compiler applies standard conversions to bring both operands to a common type before comparison:
  • Arithmetic Types: Integral promotions and usual arithmetic conversions are applied. If comparing an int and a double, the int is implicitly converted to a double prior to evaluation.
  • Pointers: Two pointers compare equal if they point to the same memory address, or if both are null pointers. Pointers to derived classes can be implicitly converted to pointers to accessible, unambiguous base classes for comparison.
  • Floating-Point: The exact behavior depends on the implementation’s floating-point representation. On systems conforming to IEEE 754 (IEC 60559), +0.0 == -0.0 evaluates to true, while NaN == NaN evaluates to false.

Operator Overloading

For user-defined types (classes, structs, unions), the == operator must be explicitly overloaded to define what constitutes equality. It can be implemented as either a member function or a non-member function. Member Function Implementation:
struct TypeA {
    int value;
    
    // The implicit 'this' pointer acts as the left-hand side operand
    bool operator==(const TypeA& rhs) const {
        return value == rhs.value;
    }
};
Note: Prior to C++20, member function overloads did not allow implicit type conversions on the left-hand operand. In C++20 and later, the compiler’s ability to rewrite expressions as rhs == lhs allows implicit conversions to apply to the left-hand operand even when implemented as a member function. Non-Member Implementation:
struct TypeB {
    int value;
    
    // Often declared as a friend if access to private members is required
    friend bool operator==(const TypeB& lhs, const TypeB& rhs) {
        return lhs.value == rhs.value;
    }
};
Note: Historically, non-member overloads were strictly required to achieve symmetric implicit conversions for both operands. While C++20 mitigates this need via rewritten candidates, non-member friends remain a common idiom.

C++20 Defaulted Equality

Starting in C++20, the == operator can be explicitly defaulted. The compiler automatically generates a short-circuiting member-wise equality comparison, evaluating base classes left-to-right, followed by non-static data members in declaration order.
struct TypeC {
    int a;
    double b;
    
    // Compiler synthesizes short-circuiting member-wise comparison
    bool operator==(const TypeC&) const = default; 
};
C++20 Overload Resolution Enhancements: When the compiler encounters lhs == rhs in C++20 and later, it performs overload resolution considering both the standard candidate (lhs == rhs) and the rewritten reversed candidate (rhs == lhs). Additionally, explicitly defining or defaulting operator== automatically enables the compiler to synthesize the inequality operator (!=), eliminating the need to write boilerplate negation logic.
Master C++ with Deep Grasping Methodology!Learn More