Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > boost::lambda question

Reply
Thread Tools

boost::lambda question

 
 
marius lazer
Guest
Posts: n/a
 
      09-15-2006
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: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;
}

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      09-15-2006

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.

 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-16-2006
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.

 
Reply With Quote
 
marius lazer
Guest
Posts: n/a
 
      09-16-2006

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

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-16-2006
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

 
Reply With Quote
 
Kaz Kylheku
Guest
Posts: n/a
 
      09-17-2006
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57