> ## 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++ Inequality

The `!=` (inequality) operator is a binary equality operator that evaluates whether its left-hand operand is not equal to its right-hand operand. It yields a `bool` prvalue: `true` if the operands differ in value, and `false` if they are equal.

```cpp theme={"dark"}
lhs != rhs
```

## Technical Characteristics

* **Return Type:** `bool`
* **Value Category:** The result is a prvalue (pure rvalue).
* **Associativity:** Left-to-right.
* **Precedence:** Level 10. It is evaluated after arithmetic operators, bitwise shift operators (`<<`, `>>`), bitwise NOT (`~`), and relational operators (`<`, `<=`, `>`, `>=`), but before bitwise AND (`&`), XOR (`^`), OR (`|`), and logical operators (`&&`, `||`).

## Built-in Type Evaluation

When applied to fundamental types, the compiler performs standard conversions (such as integral promotions or usual arithmetic conversions) to bring both operands to a common type before comparison.

* **Integral and Enumeration Types:** Performs a direct value comparison of the underlying binary representation after promotion.
* **Pointer Types:** Compares the memory addresses held by the pointers or their null pointer values. Two pointers are unequal if they point to different memory locations, or if one is a null pointer and the other is a valid pointer. Two null pointers compare equal (yielding `false` for `!=`), as they do not point to any memory location.
* **Floating-Point Types:** Compares IEEE 754 representations. Under IEEE 754 rules, `NaN` (Not-a-Number) is unordered. Consequently, if either or both operands are `NaN`, the `!=` operator evaluates to `true` (i.e., `NaN != NaN` yields `true`). Additionally, due to floating-point arithmetic precision limits, direct inequality comparisons on `float` or `double` types may yield unexpected `true` results for mathematically equivalent values.

## Operator Overloading (Pre-C++20)

For user-defined types (classes, structs, unions), the `!=` operator must be explicitly overloaded if compiled under C++17 or older. Best practice dictates implementing `operator!=` as a non-member function that negates the result of `operator==` to ensure logical consistency and allow symmetric implicit conversions on both operands.

```cpp theme={"dark"}
class Entity {
    int id;
public:
    Entity(int i) : id(i) {}

    // Implemented as a non-member friend function
    friend bool operator==(const Entity& lhs, const Entity& rhs) {
        return lhs.id == rhs.id;
    }

    // Explicit non-member overload required pre-C++20
    friend bool operator!=(const Entity& lhs, const Entity& rhs) {
        return !(lhs == rhs);
    }
};
```

## C++20 Operator Rewriting Rules

C++20 introduced operator synthesis and rewriting rules. In modern C++, explicitly overloading `operator!=` is generally obsolete if `operator==` is provided.

When the compiler encounters `lhs != rhs`, it populates the overload resolution set with both the explicitly declared `operator!=` candidates and the synthesized rewritten candidates evaluated as `!(lhs == rhs)`. Both sets of candidates are considered simultaneously. The compiler selects the best match from this combined set. A rewritten `==` candidate will be selected over a viable `!=` candidate if it is a better match (for example, if the rewritten `==` is an exact type match but the `!=` candidate requires implicit conversions).

```cpp theme={"dark"}
class ModernEntity {
    int id;
public:
    ModernEntity(int i) : id(i) {}

    // C++20: Defining == automatically provides rewritten candidates for !=
    friend bool operator==(const ModernEntity& lhs, const ModernEntity& rhs) = default; 
};

// Usage: e1 != e2 is implicitly evaluated as !(e1 == e2) via overload resolution
```

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