Boost logo

Boost Users :

Subject: Re: [Boost-users] Simple Lambda Problem
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-04-07 10:47:36


On 04/07/2011 01:02 AM, Richard Howard wrote:
> I am currently trying to get to grips with using Lambda, but I have
> hit a problem fairly early on.
>
> Can anyone explain what I am doing wrong.
>
> bool isCar(std::string const& val)
> {
> return val.find("car") != std::string::npos;
> }
>
> int main()
> {
> std::list<std::string> l;
> l.push_back("car");
> l.push_back("blue car");
> l.push_back("red van");
> l.push_back("bike");
> l.push_back("blue car");
>
> // ...
>
> l.remove_if(&isCar);
>
> // this works and is how I have done things in the past but I was
> hoping to avoid the need for the separate function by using Lambda.
> // I have tried the following
>
>
> l.remove_if(boost::lambda::var(boost::lambda::bind(&std::string::find,
> boost::lambda::_1, "car")) != std::string::npos);
>
> // I am not sure why this does not work, can anyone point me in the
> right direction?
> }
>

Don't use var. Also, I think std::string::find
is overloaded, so you'll need to cast it to
the correct member pointer type.

You can do better with phoenix which provides
find. (Phoenix 2 is included under spirit,
Phoenix 3 will be released in 1.47.) Actually,
with phoenix, you'll want to use search instead
of find, since find isn't specialized for string.

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