boost lambda binding question

B

baliga

(std::cout << _1 << _2)("hello", 10);

This does not work. If 2 parameters are string, it works. If any one or
both parameters are integers, it does not work.

But If I change it to

boost::function<void(int, int)> f = std::cout << _1 << _2;
f(10, 10);

it works.

Question is why the first one does not work when one of the parameter
is integer.

Thanx,
-- baliga

http://baliga.blogdns.com/blog
 
S

shunsuke

(std::cout << _1 << _2)("hello", 10);

This does not work. If 2 parameters are string, it works. If any one or
both parameters are integers, it does not work.

But If I change it to

boost::function<void(int, int)> f = std::cout << _1 << _2;
f(10, 10);

it works.

Question is why the first one does not work when one of the parameter
is integer.

Hi,

Lambda Functor can't accept a non-const rvalue(temporary).
See..
http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.rvalues_as_actual_arguments
http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2002/n1385.htm

Thus,
(std::cout << _1 << _2)("hello", make_const(10));
works fine.
 
S

shunsuke

"reference to array" kicks in.
The reference seems passed to 'operator<<' as is.
Then, the 'operator<<' overload of 'char const*' is selected, AFAIK.


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top