Boost logo

Boost :

From: Larry Evans (cppljevans_at_[hidden])
Date: 2005-04-26 06:58:17


On 04/26/2005 04:18 AM, Giovanni P. Deretta wrote:
> Hi,
>
> I have been using a small extension to the tuple library 'get' function,
> that I have found very handy. It allows the extraction of an element of
> a tuple by specifying its type instead of the index.
> Example:
>
> given the tuple boost::tuple<type1, type2, type3>
>
> get<type1> returns the first element of the tuple of that type.
>
> This allows one to write more self describing code: you don't need to
> look up the tuple element type when you find an indexed get function
> buried deep inside some code.
> This is especially useful with nested tuples where for example
> 'get<3>(get<5>(get<0>(my_tuple)))' looks more like line noise than real
> code :).

Of course this wouldn't work for this tuple:

   tuple<type1, type1, type3>

Maybe if there were some way of associating an enumeration with the
tuple, you could:

   get<f_i>(my_tuple)

where fi is some enumerator in, e.g.:

   enum my_tuple_fields
   { f_0
   , f_1
   ...
   , f_n
   };

but even then, with nested tuples, there's a possible name conflict
amoung the enumerators. Possibly the enumerator names could contain
a prefix indicating to which type they belong:

   enum type0_fields
   { t0_f_0
   , t0_f_1
   ...
   , t0_f_n0
   };

   enum type1_fields
   { t1_f_0
   , t1_f_1
   ...
   , t1_f_n1
   };

Obviously more verbose than I think you want, but it would solve the
problem of duplicate types.

BTW, I haven't tried it; hence, there may be some flaws in the idea ;)


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