473,473 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

boost lambda binding question

(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

Jan 26 '07 #1
3 1680
On Jan 26, 9:50 am, bal...@gmail.com wrote:
(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...tual_arguments
http://std.dkuug.dk/jtc1/sc22/wg21/d...2002/n1385.htm

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

--
Shunsuke Sogame

Jan 26 '07 #2


On Jan 25, 8:25 pm, "shunsuke" <pstade...@gmail.comwrote:
On Jan 26, 9:50 am, bal...@gmail.com wrote:
(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...2002/n1385.htm

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

--
Shunsuke Sogame
Thanx for the solution. But I still have one question. why
make_const("hello") is not required for the string or is there any
implicit conversion going on within boost?

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

--
Shunsuke Sogame
On Jan 27, 4:08 am, bal...@gmail.com wrote:
On Jan 25, 8:25 pm, "shunsuke" <pstade...@gmail.comwrote:


On Jan 26, 9:50 am, bal...@gmail.com wrote:
(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...#lambda.rvalue...
Thus,
(std::cout << _1 << _2)("hello", make_const(10));
works fine.
--
Shunsuke SogameThanx for the solution. But I still have one question. why
make_const("hello") is not required for the string or is there any
implicit conversion going on within boost?- Hide quoted text -- Show quoted text -
Jan 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
1
by: flopbucket | last post by:
Hi, After reading a bit about boost::lambda, I became curious how they implemented it. I downloaded it and had a look, but the all the headers and multiple templates make it a bit difficult to...
5
by: marius lazer | last post by:
I have an STL container of functors and I want to execute them using std::for_each. In the code snippet below the line marked "// good" works fine and the one marked "// nothing" compiles fine but...
3
by: rammel | last post by:
hi, can't I call member functions from an placeholder of a boost-like lambda expression? look on the example below please: -----------------------------------------------------------...
0
by: Barry Ding | last post by:
#include <map> #include <utility> #include <algorithm> #include <iostream> #include <list> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std;
1
by: nandor.sieben | last post by:
Let m be of type vector<vector<int. I am trying to sort each element of m: for_each(m.begin(), m.end(), bind(sort, *_1.begin(), *_1.end()); This does not work: 'const struct...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
3
by: Stuart Golodetz | last post by:
Hi, Just wondering why the following code doesn't work: #include <iostream> #include <boost/lambda/lambda.hpp> using namespace boost::lambda; int main()
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.