C++20 introduced a defaulted explicit equality operator (==
). It instructs the compiler to implement a piecewise equality check between two instances of the same class
or struct
.
As a regular member function:
#include <string>
struct X
{
int x = 0;
std::string name;
bool operator==(const X &) const = default;
};
auto main() -> int
{
X a{}, b{};
bool equal = (a == b);
return equal ? 0 : 1;
}
As a friend member function:
struct F
{
int x = 0;
std::string name;
friend bool operator==(const F &, const F &) = default;
};
auto main() -> int
{
F g{}, h{};
bool friend_equal = (g == h);
return friend_equal ? 0 : 1;
}
This default comparison replaces the manually implemented comparison and equality operator.
More information at https://en.cppreference.com/w/cpp/language/default_comparisons
See Also
- - Left Arrow Operator
- - Mistakes
- - compile_commands.json with CMake
- - Samsung Magician Stops Ctrl-w From Working
- - Default Route and Dns With Opensuse Tumbleweed with static IP
Comments
Any comments? Create a new discussion on GitHub.There used to be an inline comment form here, but it was removed.