Question about Boost.lambda example

O

ogerchikov

Hi,

I am reading a Boost.lambda example
http://www.awprofessional.com/articles/article.asp?p=400651&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.
 
A

Alf P. Steinbach

* (e-mail address removed):
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.
 
D

David Harmon

On 13 Jan 2006 23:13:45 -0800 in comp.lang.c++, (e-mail address removed)
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.
 
O

ogerchikov

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?
 
T

TB

(e-mail address removed) 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
 
D

David Harmon

On 14 Jan 2006 09:11:50 -0800 in comp.lang.c++, (e-mail address removed)
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top