
Hi folks, Is there a way to traverse a boost::tuple at compile or runtime with sth similar to foreach? Thanks, emre

Emre Turkay <emreturkay <at> gmail.com> writes:
Is there a way to traverse a boost::tuple at compile or runtime with sth similar to foreach?
Hi Emre, If you can't use boost::fusion, you can declare something like this (didn't try myself): template<class Tuple, class Func> struct tuple_binder { Tuple& t_; Func f_; tuple_binder(Tuple& t, const Func& f) : t_(t), f_(f) {} template< typename Number > void operator()(Number) { f_( t_.template get<Number::value>() ); } }; template<class Tuple, class Func> inline tuple_binder<Tuple, Func> bind_tuple(Tuple& t, const Func& f) { return tuple_binder<Tuple, Func>(t,f); } and then use boost::mpl::for_each in the following manner: MyTuple tuple; typedef boost::mpl::range_c< int, 0, boost::tuples::length<MyTuple>::value > Indices; boost::mpl::for_each< Indices >( bind_tuple( tuple, functor ) ); The idea is to iterate by tuple element indices (so you can iterate in any direction etc, just provide a desired index sequence it boost::mpl::for_each). Bests, Maxim
participants (3)
-
Emre Turkay
-
Maxim Yanchenko
-
Sebastian Redl