473,402 Members | 2,064 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,402 software developers and data experts.

boost::lambda question

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>
#include <deque>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std;
using namespace boost::lambda;

template <typename C>
void call_all(C& c)
{
std::for_each(c.begin(), c.end(), _1);
// nothing
std::for_each(c.begin(), c.end(), bind(&C::value_type::operator(),
_1)); // good
}

struct Func
{
Func(int s) : i(s) {}
void operator()() const { cerr << __PRETTY_FUNCTION__ << " " << i <<
endl; }
int i;
};

int
main()
{
deque<Funcdeq;

deq.push_back(Func(1));
deq.push_back(Func(2));
deq.push_back(Func(3));

call_all(deq);

return 0;
}

Sep 15 '06 #1
5 2273

marius lazer wrote:
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.
You might consider the boost mailing list as a better place to ask.

Sep 15 '06 #2
marius lazer wrote:
works fine and the one marked "// nothing" compiles fine but does
absolutely nothing. Can anyone explain why?
Because the lambda expression _1 is the identity function.

for each item in collection do identity

is a useless noop.

Sep 16 '06 #3

Kaz Kylheku wrote:
Because the lambda expression _1 is the identity function.

for each item in collection do identity

is a useless noop.
Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
being called. That's the piece I don't understand...

Thanks,
Marius

Sep 16 '06 #4
marius lazer wrote:
Kaz Kylheku wrote:
>Because the lambda expression _1 is the identity function.

for each item in collection do identity

is a useless noop.

Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
being called. That's the piece I don't understand...
Yes it is being called; it just does nothing. In the following, Foo()
and Bar() are equivalent:

struct Dummy { void operator()() const {} };

void Foo()
{
std::foreach(c.begin(), c.end(), _1);
}

void Bar()
{
std::foreach(c.begin(), c.end(), Dummy());
}

--
Clark S. Cox III
cl*******@gmail.com
Sep 16 '06 #5
marius lazer wrote:
Kaz Kylheku wrote:
Because the lambda expression _1 is the identity function.

for each item in collection do identity

is a useless noop.

Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
How is it relevant that the elements are functors? They aren't going to
be called. They are just passed to the identity function which returns
them, and the return value is discarded. So these elements could just
as well be integers or strings.
being called. That's the piece I don't understand...
What side effects does _1 produce by which you can tell whether or not
it is being called?

Assuming that _1 is not called, how would the behavior be different if
_1 /was/ called?

Would the compiler be wrong in deducing that _1 doesn't actually have
to be called?

Sep 17 '06 #6

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

Similar topics

3
by: Rafal Dabrowa | last post by:
Suppose I have the following structure: struct xy { int x, y; }; and the following vector: vector<xy> v(10); I want to use boost/lambda for find an item in the table. Let's say, with x...
5
by: ogerchikov | last post by:
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;...
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...
4
by: silverburgh.meryl | last post by:
I have code which uses Boost lambda in a template like this: using namespace boost::lambda; template<class T> bool lessThanXY( T& src, T& dest ) { return (src.getY() < dest.getY()); } ...
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: -----------------------------------------------------------...
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.