Boost logo

Boost :

From: jsiek_at_[hidden]
Date: 1999-11-28 16:48:05


Dave Abrahams writes:
> Duh. Or as my lovely wife would say, "crippity crap!" Is there something we
> can do to force people to implement it when they derive from the random
> access helper? Perhaps using the concept_checks technique would work.

Well, operator- is a funny case because it is not used inside of the
helper (or any of the other operators), so there isn't a good reason
to check for it there (though an algorithm that depends on operator-
should use concept checks).

But your point is a good one in general. The operators should use
concept checks to try to improve the error messages that occur
when the core operations are not there. Here's an example:

template <class T, class U = T> // = T really is needed; not a coding error
class less_than_comparable
{
     __STL_CLASS_REQUIRES_BINARY_OP(_OP_LESS_THAN, T, U);
     __STL_CLASS_REQUIRES_BINARY_OP(_OP_GREATER_THAN, U, T);

     friend bool operator<=( const T& x, const U& y ) { return !(x > y); }
     friend bool operator>=( const T& x, const U& y ) { return !(x < y); }
     friend bool operator>( const U& x, const T& y ) { return y < x; }
     friend bool operator<( const U& x, const T& y ) { return y > x; }
     friend bool operator<=( const U& x, const T& y ) { return !(y < x); }
     friend bool operator>=( const U& x, const T& y ) { return !(y > x); }
};

Cheers,

Jeremy


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk