Boost logo

Boost Users :

From: Toon Knapen (toon.knapen_at_[hidden])
Date: 2002-04-26 19:35:35


On Friday 26 April 2002 01:37 pm, Aleksey Gurtovoy wrote:
> Toon Knapen wrote:
> > I have a list of heterogeneous objects, e.g.
> >
> > struct Foo {
> > Foo(int v) : value_( v ) {}
> > int value() const { return value_ ; }
> > int value_;
> > };
> >
> > struct Bar {
> > Bar(int v) : value_( v ) {}
> > int value() const { return value_ ; }
> > int value_;
> > };
> >
> > int main()
> > {
> > Foo foo(3);
> > Bar bar(7);
> > typedef boost::tuple< Foo, Bar > MyTuple ;
> > MyTuple my_tuple( foo, bar );
> > return 0;
> > }
> >
> > And now I want to have a function that can accumulate the
> > value sof all the objects in the tuple, e.g. (pseudocode since
> > my problem is exacly that I can't figure out how to code this)
> >
> > template < typename TypleType >
> > int accumulate_values(const TupleType& t)
> > {
> > int acc = 0;
> > for(int i = 0 ; i < t.size() ; ++i ) acc += (get< i >( t )).value();
> > }
> >
> > Can I do this using mpl (mpl looks great but I can't figure
> > out how to do this).
>

[snip Aleksey's solution]

> Hope it's not too complicated :). LL might allow to do better on this one.
Thanks, I'll try it out. Coudl you enlighten me on how to solve it with the
lambda library. I don't see how to iterate over the different indexes in
combination with LL ?

t


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net