I need to create a bunch of classes so that the instances of a particular type can be mutually compared.
I thought about writing a compare method, unique to each class, and then adding the following code to each class definition, replacing T with the name of the class I'm in:
bool operator== (const T& L, const T& R) {return L.compare(R)==0;}
bool operator!= (const T& L, const T& R) {return L.compare(R)!=0;}
bool operator< (const T& L, const T& R) {return L.compare(R)<0;}
bool operator<= (const T& L, const T& R) {return L.compare(R)<=0;}
bool operator> (const T& L, const T& R) {return L.compare(R)>0;}
bool operator>= (const T& L, const T& R) {return L.compare(R)>=0;}
This is kind of repetitive, though. Is there a more generic way of doing it? I guess I could write a macro for that, parametrizing it on T, but macros are not very cplusplusy now, are they? I also thought about inheritance and polymorphism, and from what I'm read about it (I have yet to use virtual classes; I'm new to C++), it seems like I'd be introducing an unecessary runtime overhead, as I'm not going to be needing uniform access those via base class pointers. Is there a better way of accomplishing it other than macros or copy-pasting?