Default operator== in C++

At first sight, it seems surprising that there is no compiler-generated default operator== in C++, when there is a compiler-generated default assignment operator and copy constructor. It could simply do a memberwise comparison, just as the assignment operator and copy constructor do a memberwise copy.

The reason is out of compatibility with C. In C, struct comparison is illegal, so a default operator== in C++ would have made C code that shouldn’t compile as C compile, and potentially changed its behaviour. Compatibility is the same reason why C++ does have a default assignment operator and copy constructor, which is ironic given that those are rarely wanted and are often disabled by making them private.

There is a proposal to include default comparison operators in C++17, as described here: A bit of background for the default comparison proposal — Bjarne Stroustrup.


Operator overloading in C++