If I have a type that consists of a single numeric data member (say, an int
) and various methods, is there a convenient way to tell the compiler to automatically generate all the obvious comparison operators?
I.e., instead of this (using inline
instead of constexpr
for C++03, of course):
class MyValueType
{
private:
int myvalue;
public:
constexpr bool operator<(MyValueType rhs) const { return myvalue < rhs.myvalue; }
constexpr bool operator>(MyValueType rhs) const { return myvalue > rhs.myvalue; }
constexpr bool operator>=(MyValueType rhs) const { return myvalue >= rhs.myvalue; }
constexpr bool operator==(MyValueType rhs) const { return myvalue == rhs.myvalue; }
/// .... etc
}
I want something like Ruby's Comparable mixin, which basically allows you to define one operator and let Ruby take care of the rest. And I'd even assume that the compiler-generated versions would probably be better than mine: should rhs
be a const
ref for each case? Should I define versions that take forwarding references? What if I forget one of the operators? Etc.
So...does something like this exist?
(Please forgive me if this is a duplicate; I assumed someone would have already asked this somewhere, because it seems like an obvious feature to want, but I can't find any
Aucun commentaire:
Enregistrer un commentaire