tags:

views:

88

answers:

3
template <typename T>
struct Foo
{
    void operator()(T& t) { t(); }
};

Is there any standart or boost functor with the similar implementation?

I need it to iterate over container of functors:

std::for_each(beginIter, endIter, Foo<Bar>());

Or maybe there are other way to do it?

+4  A: 

Binders like Boosts or C++0x bind() make it trivial to generate such a functor:

std::for_each(begin, end, boost::bind(&Bar::operator(), _1));

Or using mem_fun_ref:

std::for_each(v.begin(), v.end(), std::mem_fun_ref(&Bar::operator()));
Georg Fritzsche
+1  A: 

It may be slightly less wordy with BOOST_FOREACH, especially if you have C++0x's auto support:

BOOST_FOREACH(auto f, v) {f();}
Cubbi
A: 

At least i found it. boost::apply should do all the job

std::for_each(beginIter, endIter, boost::bind(boost::apply<void>(), _1));
Voivoid