C++ cannot infer this automatically for a couple of reasons:
- It doesn’t make sense for every single type to be compared with
operator<, so the type may not necessarily define aoperator<.- This means that
operator==cannot be automatically defined in terms ofoperator<
- This means that
operator<isn’t required to compare its arguments. A programmer can define operators for their types to do almost anything to their arguments.- This means that your statement about
!(a < b) && !(b < a)being equivalent toa == bmay not necessarily be true, assuming those operators are defined.
- This means that your statement about
If you want an operator== function for your types, just define one yourself. It’s not that hard 🙂
// For comparing, something like this is used
bool operator==(const MyType& lhs, const MyType& rhs)
{
// compare (or do other things!) however you want
}
// ... though it's not the only thing you can do
// - The return type can be customised
// - ... as can both of the arguments
const MyType& operator==(int* lhs, const MyType* const rhs)
{
return lhs;
}