CKTIS / C++
Equality operator ==
The == operator tests operands for equality. Built-in comparison returns bool, while classes can define comparison through operator==. Do not confuse == with assignment = or pointer comparison with comparison of the pointed-to values.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
lhs == rhsThe expression lhs == rhs tests operands for equality. The built-in operator returns bool: either true or false. Do not confuse it with =, which assigns a value and can change the left operand.
Numeric comparisons may perform arithmetic conversions, so the operands need not initially have identical types. Pointer comparison does not compare the pointed-to values; those values must be accessed separately to compare them.
For classes, an overloaded operator== can define what == means; a custom implementation need not return bool. However, the example's declaration bool operator==(const Point&) const = default; returns bool and compares the corresponding members x and y. The result is true only when both pairs are equal. In general, defaulted comparison also includes base-class subobjects; it can be defined as deleted if required comparisons are unavailable.
In the example, a and b have equal members, while c differs in its y member. The expected output is three lines: true, true, false. The supplied source is an evolving working draft, not a frozen C++23 publication.
Examples
#include <iostream>
struct Point {
int x;
int y;
bool operator==(const Point&) const = default;
};
int main() {
const Point a{2, 3};
const Point b{2, 3};
const Point c{2, 4};
std::cout << std::boolalpha;
std::cout << (7 == 7) << '\n';
std::cout << (a == b) << '\n';
std::cout << (a == c) << '\n';
}
Result
true true false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.