Loki's Visitor and for_each

T

trash

I do this kindda stuff often:

class CVisitableObjectVector : public
std::vector<Loki::BaseVisitable<>*> {

void Enable(bool enable);
....
};


void CVisitableObjectVector::Enable(bool enable) {

CLokiVisitor Visitor(enable);

CVisitableObjectVector::iterator i = begin();

for (; i != end(); i++) {
(*i)->Accept(Visitor);
}

}

Can that for(;;;) loop be replaced with the std::for_each algorithm?

I've tried with no success. I've used ptr_fun, mem_fun etc.. Nothing
works. Has anyone out there done this?
 
P

Phil Staite

Maybe something like this - sorry, don't have time to compile it and try it:


#include<function>
#include<algorithm>

using namespace std; // just to keep example short


for_each( begin(),
end(),
bind2nd( mem_fun1_t<CVisitableObjectVector,CLokiVisitor>(
Accept ), Visitor ) );


The idea is, you want the mem_fun1_t because you want to call a member
function via a pointer, and it takes one argument. However, since
for_each passes only the container element to the member function (or
functor) you use bind2nd to bind the Visitor to a functor first... Or
some such...
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top