I've defined an IntWrapper class like the following one:
struct IntWrapper
{
protected:
int value;
public:
explicit IntWrapper() = default;
explicit IntWrapper(const int value) : value(value) {}
bool operator< (const IntWrapper rhs) const { return value < rhs.value; }
bool operator> (const IntWrapper rhs) const { return value > rhs.value; }
bool operator<=(const IntWrapper rhs) const { return value <= rhs.value; }
bool operator>=(const IntWrapper rhs) const { return value >= rhs.value; }
bool operator==(const IntWrapper rhs) const { return value == rhs.value; }
explicit operator int() const { return value; }
};
and the classes Foo and Bar that inherit from IntWrapper
struct Foo: IntWrapper
{
using IntWrapper::IntWrapper;
};
struct Bar: IntWrapper
{
using IntWrapper::IntWrapper;
};
I'd like to compare only objects of the same type. In other words I'd like the following piece give compilation error, instead of casting foo and bar to IntWrapper.
const Foo foo(1);
const Bar bar(2);
bool b = foo >= bar;
Since I have many other objects like Foo and Bar, in there any way to achieve my result keeping all the comparison operators inside IntWrapper?