Boost logo

Boost :

From: Yuval Ronen (ronen_yuval_at_[hidden])
Date: 2006-04-05 13:46:47


Yuval Ronen wrote:
> #define MY_STRUCT_AS_TUPLE(obj) const_tie(obj.x, obj.y, obj.z)
>
> bool operator<(const MyStruct &lhs, const MyStruct &rhs)
> {
> return MY_STRUCT_AS_TUPLE(lhs) < MY_STRUCT_AS_TUPLE(rhs);
> }

Nobody corrected me, but this code works just fine with the existing
boost::tie. There's no need for const_tie. I even prepared a macro to
ease the creation of comparison operators:

     #define IMPLEMENT_COMPARISON_OPERATOR(TYPE, MAKE_COMPARABLE_MACRO,
OPERATOR) \
     bool operator OPERATOR(const TYPE &a_lhs, const TYPE &a_rhs) \
     { \
         return MAKE_COMPARABLE_MACRO(a_lhs) OPERATOR
MAKE_COMPARABLE_MACRO(a_rhs); \
     }

which I can use as (if I have a struct by the name MyStruct):

     #define MY_STRUCT_AS_TUPLE(obj) tie(obj.x, obj.y, obj.z)
     IMPLEMENT_COMPARISON_OPERATOR(MyStruct, MY_STRUCT_AS_TUPLE, ==)
     IMPLEMENT_COMPARISON_OPERATOR(MyStruct, MY_STRUCT_AS_TUPLE, <)

I think this is a very convenient way of creating comparison operators,
if anyone is interested, and maybe it will even be a good idea to add it
to the tuples documentation as "possible usages".

Hope it was useful,
Yuval


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