loop -> for_each advanced

I

ibe

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
 
V

Victor Bazarov

ibe said:
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
 
J

Jeff Flinn

ibe said:
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
 
I

ibe


yes, of course.
[all other typos snipped]

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

yes it does, but i wanna get the..

....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
 
I

ibe

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
 
L

Luke Meyers

ibe said:
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
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top