The compiler will not do anything for you here, but it is relatively simple to create it automatically, inheriting from the corresponding class, something like:
template< typename DerivedType > class ComparisonOperators { public: friend bool operator!=( DerivedType const& lhs, DerivedType const& rhs ) { return !(lhs == rhs); } friend bool operator<=( DerivedType const& lhs, DerivedType const& rhs ) { return !(rhs < lhs); } friend bool operator>( DerivedType const& lhs, DerivedType const& rhs ) { return rhs < lhs; } friend bool operator>=( DerivedType const& lhs, DerivedType const& rhs ) { return !(lhs < rhs); } protected: ~ComparisonOperators() {} } ;
Define < and == in your class and extract from this, and you will get all the operators:
class MyClass : public ComparisonOperators<MyClass> { // ... public: bool operator==( MyClass const& other ) const; bool operator<( MyClass const& other ) const; // ... };
Just a note: I manually simplified the version used, which defines == and < , and also looks for a member of the compare and isEqual , and uses compare for == and != When there is no isEqual . I donβt think I made any mistakes, but you never know.
source share