I'm trying to do something like this:
using namespace boost::lambda;
using boost::thread;
int add(int a, int b) {return a+b;}
int sum, x=2, y=6;
thread adder(var(sum) = add(_1, _2), x, y);
adder.join();
cout << sum;
I get a compile error:
cannot convert parameter 1 from 'boost::arg' to 'int'
You're really close actually! The problem is that you're directly calling add() with Lambda's placeholders—it's not being evaluated lazily inside the lambda, but right away.
Here's a fixed version:
using namespace boost::lambda;
using boost::thread;
int sum, x=2, y=6;
thread adder(var(sum) = _1 + _2, x, y);
adder.join();
cout << sum;
And if you really want to use the add
function, you'd use bind
:
using namespace boost::lambda;
using boost::thread;
int add(int a, int b) {return a+b;}
int sum, x=2, y=6;
thread adder(var(sum) = bind(add, _1, _2), x, y);
adder.join();
cout << sum;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.