Sign in to follow this  
Zbychs

boost::lambda question

Recommended Posts

Hi This works:
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " );
And now I'm trying to do something like this:
std::for_each( str.begin(), str.end(), std::cout << _totupper(_1) << " " );
It doesn't work. Any ideas how I can fix it?

Share this post


Link to post
Share on other sites
Been a while since I used boost::lambda.

Try replacing _1 with boost::var(_1).

I vaguely recall something like that. Also, note that boost.lambda is going to be deprecated eventually since it's superseded by boost.phoenix, which currently isnt't have an independent library, it is distributed as part of boost.spirit so you can find the documentation there.

Share this post


Link to post
Share on other sites
Thanks cache_hit, but it still doesn't work.

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'boost::_bi::bind_t<R,F,L>'

Any other ideas?

Share this post


Link to post
Share on other sites
Aha, boost::lambda and boost::bind use incompatible placeholders. Replace _1 with boost::lambda::_1. Generally speaking #include'ing boost/bind.hpp and anything from lambda in the same translation unit can cause problems due to placeholder issues

Share this post


Link to post
Share on other sites
Quote:
Original post by Zbychs
It doesn't work.
Any ideas how I can fix it?


What's _totupper? Is it smart enough to delay the evaluation of that function until inside the 'loop'? In other words, does _totupper(_1) return a functor?

You'll probably have to use boost::lambda::bind:


// assuming _totupper is a function
using boost::lambda::bind;
using boost::lambda::_1;
std::for_each( str.begin(), str.end(), std::cout << bind(&_totupper, _1) << " " );

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this