TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
!= (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.
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
falsefor!=), 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 areNaN, the!=operator evaluates totrue(i.e.,NaN != NaNyieldstrue). Additionally, due to floating-point arithmetic precision limits, direct inequality comparisons onfloatordoubletypes may yield unexpectedtrueresults 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.
C++20 Operator Rewriting Rules
C++20 introduced operator synthesis and rewriting rules. In modern C++, explicitly overloadingoperator!= 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).
Master C++ with Deep Grasping Methodology!Learn More





