From "Modernes C++":
by Rainer Grimm
From the article:
A value object is a small object whose equality is based on state, but not identity. Typical value objects are money, numbers, or strings. ...
... For a user-defined type, you have to choose the appropriate equality semantics. ... In C++20, the compiler can auto-generate the equality operator and use it as a fallback for the inequality operator. The auto-generated equality operator applies value equality. To be more precise, the compiler-generated equality operator performs a lexicographical comparison. Lexicographical comparison means that all base classes are compared left to right and all nonstatic members of the class in their declaration order.
I have to add two important points:
- For strings or vectors, there is a shortcut: the compiler-generated == and != operators compare first their lengths and then their content if necessary.
- The compiler-generated three-way comparison operator (
<=>) also applies value equality. You can read more about the three-way comparison operator in my previous posts:
Honestly, the example works as expected but does not seem right. ...