By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

boost::lambda question

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.