|
Boost Users : |
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-04-17 20:14:00
AMDG
Robert Dailey wrote:
> Hi,
>
> In the Boost.Lambda documentation, it has a section for operators that
> cannot be overloaded, but I'm not really sure if this means you cannot
> use these operators with lambda expressions.
>
> I have the following map:
>
> class SomeClass
> {
> public:
> void Foo() {}
> };
>
> std::map<int, SomeClass> mymap;
>
> I want to be able to do std::for_each() on this map and use lambdas to
> call the Foo() member on each element in the map. For example:
>
> std::for_each( mymap.begin(), mymap.end(), _1.second.Foo() );
>
> Obviously the above does not compile. Is there a way to make this work
> without doing some crazy template specialization? Thanks.
It's pretty ugly:
#include <map>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
class SomeClass
{
public:
void Foo() {}
};
int main() {
using boost::lambda::bind;
using boost::lambda::_1;
std::map<int, SomeClass> mymap;
std::for_each( mymap.begin(), mymap.end(),
bind(&SomeClass::Foo, bind(&std::pair<const int,
SomeClass>::second, _1)) );
}
You can do the same thing using Boost.Bind, too.
In Christ,
Steven Watanabe
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