![](/img/trans.png)
[英]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.