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 != (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.
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.
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).
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
Master C++ with Deep Grasping Methodology!Learn More