[英]Why are the STL functors themselves templated and not their function call operator?
[英]any stl/boost functors to call operator()
template <typename T>
struct Foo
{
void operator()(T& t) { t(); }
};
是否有類似實現的標准或增強仿函數?
我需要它迭代仿函數容器:
std::for_each(beginIter, endIter, Foo<Bar>());
或者也許有其他方法可以做到這一點?
像Boosts或C ++ 0x bind()
這樣的bind()
器使生成這樣的仿函數變得微不足道:
std::for_each(begin, end, boost::bind(&Bar::operator(), _1));
或者使用mem_fun_ref
:
std::for_each(v.begin(), v.end(), std::mem_fun_ref(&Bar::operator()));
BOOST_FOREACH
可能略顯冗長,特別是如果你有C ++ 0x的自動支持:
BOOST_FOREACH(auto f, v) {f();}
至少我發現了它。 boost :: apply應該完成所有工作
std::for_each(beginIter, endIter, boost::bind(boost::apply<void>(), _1));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.