473,698 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_b ack(1);
myvector.push_b ack(2);
myvector.push_b ack(4);
for_each(myvect or.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 5055
* og********@gmai l.com:

for_each(myvect or.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********@gmai l.com
wrote,
for_each(myvect or.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********@gmai l.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********@gmai l.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
4022
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
5
2288
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 does absolutely nothing. Can anyone explain why? I'm using gcc 4.1.1 on Solaris8. Thanks, Marius #include <iostream>
3
1696
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 boost::function<void(int, int)f = std::cout << _1 << _2; f(10, 10);
3
3789
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: ----------------------------------------------------------- #include <iostream> #include <algorithm> #include <iterator>
4
5061
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; typedef std::map<int,std::string_ttype; type test;
1
2250
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 boost::lambda::lambda_functor<boost::lambda::placeholder<1' has no member named 'begin'
4
3159
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 <boost/lambda/lambda.hpp>
1
2625
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 boost::lambda::lambda_ functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
3
2213
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
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2342
muto222
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.