Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost function and boost lambda

I've seen a few related questions, but am still just puzzled. What's wrong with this syntax:

boost::function<int (int)> g = f;
boost::function<int (int)> g2 = 2*g(boost::lambda::_1);

I've tried it with boost 1.35 and 1.38 (these are the two installations I have lying around) on gcc 4.3.4, and they both give variations of the error:

no match for call to '(boost::function<int ()(int)>) (const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >&)'
like image 944
pythonic metaphor Avatar asked Dec 28 '22 01:12

pythonic metaphor


2 Answers

You can't call a function with a placeholder directly. You have to use bind.

boost::function<int (int)> g2 = 2 * boost::lambda::bind(g, boost::lambda::_1);

(Example)

like image 61
kennytm Avatar answered Jan 03 '23 05:01

kennytm


I suggest you give up Boost.Lambda as it’s out of date. The compiler which supports C++0x provides native lambda and is easier to understand. You can use GCC with 4.4 or higher version, Visual Studio 2010 also supports C++0x.

like image 39
Ruimin Shen Avatar answered Jan 03 '23 05:01

Ruimin Shen