Boost logo

Boost :

From: Maxim Yanchenko (maximyanchenko_at_[hidden])
Date: 2008-04-09 04:24:03

Emre Turkay <emreturkay <at>> 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
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>
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,
> 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).


Boost list run by bdawes at, gregod at, cpdaniel at, john at