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.
== 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
- 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
intand adouble, theintis implicitly converted to adoubleprior 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.0evaluates totrue, whileNaN == NaNevaluates tofalse.
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:
rhs == lhs allows implicit conversions to apply to the left-hand operand even when implemented as a member function.
Non-Member Implementation:
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.
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





