any stl/boost functors to call operator()
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 o开发者_运维百科f functors:
std::for_each(beginIter, endIter, Foo<Bar>());
Or maybe there are other way to do it?
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()));
It may be slightly less wordy with BOOST_FOREACH
, especially if you have C++0x's auto support:
BOOST_FOREACH(auto f, v) {f();}
At least i found it. boost::apply should do all the job
std::for_each(beginIter, endIter, boost::bind(boost::apply<void>(), _1));
精彩评论