loop -> for_each advanced

Discussion in 'C++' started by ibe, Jan 20, 2006.

  1. ibe

    ibe Guest

    Hi,
    i can't get the clue:
    I have had a loop in my code like this:

    ---snip---

    class Foo {
    accept(Visitor& v) {
    v.visit(*this);
    }
    };

    class Visitor {
    visit (Foo& f) {
    ...
    }
    }

    ....in main...

    Visitor v(arugments, ...);
    for (std::deque<Foo*>::iterator iter = list_.begin();
    iter != m_list.end();
    ++iter) {
    (*iter)->accept(v);
    }

    ---snip---

    by means of the boost::bind i could express the loop with

    std::for_each(list_.begin(), list_.end(),
    bind(mem_fun(&Foo::accept), _1, v));

    fine!
    But now things changed to something like

    std::deque<boost::shared_ptr<Foo> > list_;

    I dont have an idea how could i express the same loop under this
    curcumstances.
    Can anyone help me?

    thx in advance
    Bernhard
     
    ibe, Jan 20, 2006
    #1
    1. Advertising

  2. ibe wrote:
    > i can't get the clue:
    > I have had a loop in my code like this:
    >
    > ---snip---
    >
    > class Foo {


    public:

    > accept(Visitor& v) {


    void accept(Visitor& v) {

    > v.visit(*this);
    > }
    > };
    >
    > class Visitor {


    public:

    > visit (Foo& f) {
    > ...
    > }
    > }

    ;
    >
    > ...in main...
    >
    > Visitor v(arugments, ...);
    > for (std::deque<Foo*>::iterator iter = list_.begin();
    > iter != m_list.end();
    > ++iter) {
    > (*iter)->accept(v);


    That should work for your new solution as well...

    > }
    >
    > ---snip---
    >
    > by means of the boost::bind i could express the loop with
    >
    > std::for_each(list_.begin(), list_.end(),
    > bind(mem_fun(&Foo::accept), _1, v));
    >
    > fine!
    > But now things changed to something like
    >
    > std::deque<boost::shared_ptr<Foo> > list_;
    >
    > I dont have an idea how could i express the same loop under this
    > curcumstances.
    > Can anyone help me?


    Doesn't it still compile? If it doesn't what error message do you get?
    Post the minimal complete compilable code that exhibits the problem you
    are talking about.

    V
     
    Victor Bazarov, Jan 20, 2006
    #2
    1. Advertising

  3. ibe

    Jeff Flinn Guest

    ibe wrote:
    > Hi,
    > i can't get the clue:
    > I have had a loop in my code like this:
    >
    > ---snip---
    >
    > class Foo {
    > accept(Visitor& v) {
    > v.visit(*this);
    > }
    > };
    >
    > class Visitor {
    > visit (Foo& f) {
    > ...
    > }
    > }
    >
    > ...in main...
    >
    > Visitor v(arugments, ...);
    > for (std::deque<Foo*>::iterator iter = list_.begin();
    > iter != m_list.end();
    > ++iter) {
    > (*iter)->accept(v);
    > }
    >
    > ---snip---
    >
    > by means of the boost::bind i could express the loop with
    >
    > std::for_each(list_.begin(), list_.end(),
    > bind(mem_fun(&Foo::accept), _1, v));
    >
    > fine!
    > But now things changed to something like
    >
    > std::deque<boost::shared_ptr<Foo> > list_;


    std::for_each
    ( list_.begin()
    , list_.end()
    , bind( &Foo::accept, _1, v )
    );

    should do it. Note that with the above, v is copied. Alternatively pass
    boost::ref(v) to pass a reference to your v.

    Jeff
     
    Jeff Flinn, Jan 20, 2006
    #3
  4. ibe

    ibe Guest

    On Fri, 20 Jan 2006 11:37:51 -0500, Victor Bazarov wrote:

    > ibe wrote:
    >> class Foo {

    >
    > public:


    yes, of course.
    [all other typos snipped]


    >> Visitor v(arugments, ...);
    >> for (std::deque<Foo*>::iterator iter = list_.begin();
    >> iter != m_list.end();
    >> ++iter) {
    >> (*iter)->accept(v);

    >
    > That should work for your new solution as well...


    yes it does, but i wanna get the..

    >> std::for_each(list_.begin(), list_.end(),
    >> bind(mem_fun(&Foo::accept), _1, v));


    ....thing work

    > Doesn't it still compile? If it doesn't what error message do you get?
    > Post the minimal complete compilable code that exhibits the problem you
    > are talking about.


    it can't bind any more, because the iteraters are now of type shared_ptr
    and no raw pointers.

    B
     
    ibe, Jan 20, 2006
    #4
  5. ibe

    ibe Guest

    On Fri, 20 Jan 2006 11:42:48 -0500, Jeff Flinn wrote:

    > std::for_each
    > ( list_.begin()
    > , list_.end()
    > , bind( &Foo::accept, _1, v )
    > );


    you are absolutely right! std::mem_fun is too much.
    Thanks a lot for the hint with boost::ref too!

    B
     
    ibe, Jan 20, 2006
    #5
  6. ibe

    Luke Meyers Guest

    ibe wrote:
    > it can't bind any more, because the iteraters are now of type shared_ptr
    > and no raw pointers.


    Try boost::mem_fn instead of std::mem_fun. It handles smart pointers
    transparently, IIRC.

    Luke
     
    Luke Meyers, Jan 20, 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. ibe

    loop -> for_each

    ibe, Jan 13, 2006, in forum: C++
    Replies:
    1
    Views:
    355
    Thomas Jakob
    Jan 13, 2006
  2. ibe

    loop -> for_each

    ibe, Jan 20, 2006, in forum: C++
    Replies:
    0
    Views:
    345
  3. Sarath
    Replies:
    13
    Views:
    727
    V.R. Marinov
    Jun 5, 2007
  4. Michele Simionato
    Replies:
    1
    Views:
    611
    Lacrima
    Mar 27, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    405
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page