boost::lambda question

M

marius lazer

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::eek:perator(),
_1)); // good
}

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

int
main()
{
deque<Func> deq;

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

call_all(deq);

return 0;
}
 
N

Noah Roberts

marius said:
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.
 
K

Kaz Kylheku

marius said:
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.
 
M

marius lazer

Kaz said:
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
 
C

Clark S. Cox III

marius said:
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());
}
 
K

Kaz Kylheku

marius said:
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?
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top