views:

23

answers:

1

Hello,

I've implemented the functionality of std::rel_ops namespace as a template base class (it defines all comparison operators using only operators < and ==). For me it's a bit weird that it works (so far) properly, also I'm concerned about the 'hacks' used. Can anyone assess the following code and say if I'm just lucky it to work or it's standard practice to do things like that.

template <typename T>
class RelationalOps {
public:

    inline bool operator!=(const T &rhs) const
    {
        const T& lhs = static_cast<const T&>(*this);
        return !(lhs == rhs);
    }

    inline bool operator<=(const T &rhs) const
    {
        const T& lhs = static_cast<const T&>(*this);
        return ((lhs < rhs) || (lhs == rhs));
    }

    inline bool operator>(const T &rhs) const
    {
        const T& lhs = static_cast<const T&>(*this);
        return !((lhs < rhs) || (lhs == rhs));
    }

    inline bool operator>=(const T &rhs) const
    {
        const T& lhs = static_cast<const T&>(*this);
        return !(lhs < rhs);
    }
};
+1  A: 

Well, why not use Boost.Operators?

usta