
Hi. In the tuples library there is a function that creates a tuple with auto deduction of types - make_tuple(), and there's also a function that creates a tuple with reference of auto deduction of types - tie(), but there's no function that does the same with const reference. I found that such a function is useful when I have a simple struct with simple members, and I want comparision operators to it. Instead of writing these operators myself, I'd like to use tuples' code that does it. To ease this task (IOW, to save me specifying the tuple type explicitly) a const_tie would be most useful. my code would then look like: #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); } which is the best I can think of (with typeof I could also turn the macro to a template function). I know I can accomplish the same with make_tuple() and cref(), but this way I can avoid the repetitive cref() calls. Can this be added? Thanks, Yuval

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
participants (1)
-
Yuval Ronen