473,412 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Question about Boost.lambda example

Hi,

I am reading a Boost.lambda example
http://www.awprofessional.com/articl...00651&seqNum=4.

Here is the example:

void demoVector() {
vector<int> myvector;
myvector.push_back(1);
myvector.push_back(2);
myvector.push_back(4);
for_each(myvector.begin(), myvector.end(),
cout << _1 << '\n'
);
}

My question is how can this compile? the last argument of for_each
takes a UnaryFunction.
how can I just type in " cout << _1 << '\n'" as the last argument of
for_each?

Thank you.

Jan 14 '06 #1
5 5044
* og********@gmail.com:

for_each(myvector.begin(), myvector.end(),
cout << _1 << '\n'
);
}

My question is how can this compile? the last argument of for_each
takes a UnaryFunction.
how can I just type in " cout << _1 << '\n'" as the last argument of
for_each?


Short answer: template magic.

Longer answer: you know the argument should be UnaryFunction. Hence the
result of Magic<<'\n' is an object that either is or converts to a
UnaryFunction. In turn that implies that Whatever<<_1 produces a Magic
object. So it's probably a templated operator<<. And so on.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 14 '06 #2
On 13 Jan 2006 23:13:45 -0800 in comp.lang.c++, og********@gmail.com
wrote,
for_each(myvector.begin(), myvector.end(),
cout << _1 << '\n'
);
}

My question is how can this compile? the last argument of for_each
takes a UnaryFunction.
how can I just type in " cout << _1 << '\n'" as the last argument of
for_each?


You will have to supply the details, here is a general idea.
The magic starts with _1. _1 is an object from the boost lambda
library of some tricksy type. operator<<(std::iostream&, tricksy)
produces an object of another tricksy type. And the next <<'\n'
invokes an operator that produces a new object of some tricksy type.

That final object is passed to std::for_each, where eventually it's
operator() is called. And that operator() uses information saved
away when the object was built, to evaluate the thing you originally
wanted.
Jan 14 '06 #3
So basically boost.lambda library only works if I want to print each
element to the "count" stream with a '\n'? Can i use it for something
else? The examles I found are all for printing the element in the stl
container.

Please tell me where I can find example for some other usage?

Jan 14 '06 #4
TB
og********@gmail.com sade:
So basically boost.lambda library only works if I want to print each
element to the "count" stream with a '\n'? Can i use it for something
else? The examles I found are all for printing the element in the stl
container.

Please tell me where I can find example for some other usage?


www.boost.org

TB
Jan 14 '06 #5
On 14 Jan 2006 09:11:50 -0800 in comp.lang.c++, og********@gmail.com
wrote,
So basically boost.lambda library only works if I want to print each
element to the "count" stream with a '\n'? Can i use it for something
else? The examles I found are all for printing the element in the stl
container.


Huh? No, most usage has nothing to do with output.

The first four examples at boost.org->libraries->
lambda->Introductory Examples have nothing to do with output.

Jan 15 '06 #6

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

Similar topics

6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
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: baliga | last post by:
(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 ...
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: -----------------------------------------------------------...
4
by: yurec | last post by:
Hi I start learning boost.Today I tried to use boost::lambda but failed to compile very simple example. 2 hours of googling gave nothing. Can anybody help me? using namespace boost::lambda; ...
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...
4
by: Jerzie.Klenchier | last post by:
Hi all, I'm having some difficulty in getting the following piece of code to compile: #include <iostream> #include <deque> #include <algorithm> #include <boost/tuple/tuple.hpp> #include...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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...

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.