Boost logo

Boost :

From: David_Abrahams_at_[hidden]
Date: 1999-06-30 14:48:13


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!)

-Dave

----------

// Copyright (C) David Abrahams 1999. Permission to copy, use,
// modify, sell and distribute this software is granted provided
this
copyright
// notice appears in all copies. This software is provided "as is"
without
// express or implied warranty, and with no claim as to its
suitability for
// any purpose.

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); }
};

//

template <class T, class U = T>
class multipliable
{
     friend T operator*( T x, const U& y ) { return x *= y; }
     friend T operator*( const U& y, T x ) { return x *= y; }
};

template <class T>
class multipliable<T, T>
{
     friend T operator*( T x, const T& y ) { return x *= y; }
};

template <class T, class U = T>
class addable
{
     friend T operator+( T x, const U& y ) { return x += y; }
     friend T operator+( const U& y, T x ) { return x += y; }
};

template <class T>
class addable<T, T>
{
     friend T operator+( T x, const T& y ) { return x += y; }
};

template <class T, class U = T>
class subtractable
{
     friend T operator-( T x, const U& y ) { return x -= y; }
};

template <class T, class U = T>
class divideable
{
     friend T operator-( T x, const U& y ) { return x /= y; }
};

template <class T, class U = T>
class modable
{
     friend T operator-( T x, const U& y ) { return x %= y; }
};

template <class T, class U = T>
class xorable
{
     friend T operator^( T x, const U& y ) { return x ^= y; }
     friend T operator^( const U& y, T x ) { return x ^= y; }
};

template <class T>
class xorable<T, T>
{
     friend T operator^( T x, const T& y ) { return x ^= y; }
};

template <class T, class U = T>
class andable
{
     friend T operator&( T x, const U& y ) { return x &= y; }
     friend T operator&( const U& y, T x ) { return x &= y; }
};

template <class T>
class andable<T, T>
{
     friend T operator&( T x, const T& y ) { return x &= y; }
};

template <class T, class U = T>
class orable
{
     friend T operator|( T x, const U& y ) { return x |= y; }
     friend T operator|( const U& y, T x ) { return x |= y; }
};

template <class T>
class orable<T, T>
{
     friend T operator|( T x, const T& y ) { return x |= y; }
};

}

------------------------------------------------------------------------

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