boost::lambda question

Discussion in 'C++' started by marius lazer, Sep 15, 2006.

  1. marius lazer

    marius lazer Guest

    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;
    }
     
    marius lazer, Sep 15, 2006
    #1
    1. Advertising

  2. marius lazer

    Noah Roberts Guest

    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.
     
    Noah Roberts, Sep 15, 2006
    #2
    1. Advertising

  3. marius lazer

    Kaz Kylheku Guest

    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.
     
    Kaz Kylheku, Sep 16, 2006
    #3
  4. marius lazer

    marius lazer Guest

    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
     
    marius lazer, Sep 16, 2006
    #4
  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
    > 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
     
    Clark S. Cox III, Sep 16, 2006
    #5
  6. marius lazer

    Kaz Kylheku Guest

    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?
     
    Kaz Kylheku, Sep 17, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Suzi
    Replies:
    13
    Views:
    608
    Bengt Richter
    Jan 7, 2005
  2. Toby Bradshaw
    Replies:
    6
    Views:
    1,759
    Kai-Uwe Bux
    Jun 2, 2006
  3. Misiu
    Replies:
    3
    Views:
    2,390
    Misiu
    Jan 31, 2007
  4. Replies:
    0
    Views:
    594
  5. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    178
    Sean O'Halpin
    Mar 30, 2009
Loading...

Share This Page