> ## 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.

# C++ Equality

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

```cpp theme={"dark"}
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:**

```cpp theme={"dark"}
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:**

```cpp theme={"dark"}
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.

```cpp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
