Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 1999-07-01 04:35:35


David_Abrahams_at_[hidden] wrote:
>
> I would like to get feedback on following operator templates which I
> am polishing for boost.
>
> The purpose of these templates is to define global operators in terms
> of
> member operators like operator+=().
>
> For example, you declare your class like this:
>
> struct foo : boost::addable<foo>
> {
> const foo& operator+=( const foo& );
> };
>
> and then the templates do all the work for the various operator+
> functions you would also want.
>
> The above front matter graciously suggested by Beman Dawes.
>
> There is plenty of room for critique of these, in particular the
> non-commutative operators /,%,-, and the two-type version of
> less_than_comparable. Also the names (more fun w/name haggling!)

> namespace boost {
>
> // The two-type version requires that T implement
> // bool operator<( const U& ) const, bool operator>( const U& ) const
> template <class T, class U = T>
> class less_than_comparable
> {
> friend bool operator<=( const T& x, const T& y ) { return !(x >
> y); }
> friend bool operator>=( const T& x, const T& 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); }
> };
>
> // This partial specialization requires only that T implement
> // bool operator<( const T& ) const
> template <class T>
> class less_than_comparable<T, T>
> {
> friend bool operator>( const T& x, const T& y ) { return y < x;
> }
> friend bool operator<=( const T& x, const T& y ) { return !(y <
> x); }
> friend bool operator>=( const T& x, const T& y ) { return !(x <
> y); }
> };
>
> template <class T, class U = T>
> class equality_comparable
> {
> friend bool operator==( const U& y, const T& x ) { return x ==
> y; }
> friend bool operator!=( const U& y, const T& x ) { return !(x ==
> y); }
> friend bool operator!=( const T& y, const U& x ) { return !(y ==
> x); }
> };
>
> template <class T>
> class equality_comparable<T, T>
> {
> friend bool operator!=( const T& x, const T& y ) { return !(x ==
> y); }
> };

I have written such classes but with just one type T.

(See
http://www.eleves.ens.fr:8080/home/bonnard/NewLibs/Utils/comparison_ops.H)

I have other similar stuff for the STL:

in http://www.eleves.ens.fr:8080/home/bonnard/NewLibs/Utils/
see *_utils.H

After a cleanup I will submit these heades. You can already
comment them.

-- 
Valentin Bonnard
------------------------------------------------------------------------
eGroups.com home: http://www.egroups.com/group/boost
http://www.egroups.com - Simplifying group communications

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