RE: [Boost-Users] iterating over a tuple

Toon Knapen wrote:
It's not exactly a part of MPL (the library doesn't target "run-time" stuff to that extent yet; I hope it will some day), but "tuple extension" components I wrote some time ago (http://groups.yahoo.com/group/boost/files/tuple_ext/tuple_ext-08-nov-01.zip ) allow one to do exactly these kinds of things: // untested! :) template< typename Seq, typename T, typename BinaryOp > T accumulate(Seq const& s, T const& Init, BinaryOp op) { T result = Init; for_each( begin(s) , end(s) , boost::bind<T>(op, boost::ref(result), _1) ); return result; } With the above, you can do something like this: struct get_value { template< typename T > int operator()(T const& x) const { return x.value(); } }; int sum = accumulate( tuple , 0 , boost::bind(std::plus<int>() , _1 , boost::bind<int>(get_value(),_2) ) ); Hope it's not too complicated :). LL might allow to do better on this one. Aleksey

On Friday 26 April 2002 13:37, Aleksey Gurtovoy wrote:
Thanks, I like tuple_ext a _lot_ ! It would be great to have complete STL-like functionality like this. I created an accumulate function (similar to the STL one) (see below) I also had to change your for_each. Actually, as in the STL, the unary-function does not necessarily need to be const so I removed the const. And finally, I also removed the friend declaration in the tuple_iterator since it complains about an implicit friend declaration to itself when using an iterator in a const-member function (gcc 2.95.3 and 3.x) Plans on adding this tuple_ext to the CVS ? toon [Non-text portions of this message have been removed]
participants (2)
-
Aleksey Gurtovoy
-
Toon Knapen