调用容器中的所有仿函数

Beg*_*oth 3 c++11

我有一系列std :: function对象(一种非常原始的信号系统形式).是否有一个标准(在C++ 0x中)函数或函数将调用给定的std :: function?现在我用

std::for_each(c.begin(), c.end(), 
              std::mem_fn(&std::function<void ()>::operator()));
Run Code Online (Sandbox Code Playgroud)

恕我直言,这std::mem_fn(&std::function<void ()>::operator())很难看.我希望能够写作

std::for_each(c.begin(), c.end(), funcall);
Run Code Online (Sandbox Code Playgroud)

有这样的funcall吗?或者我可以实现一个功能

template<typename I>
void callInSequence(I from, I to)
{
  for (; from != to; ++from) (*from)();
}
Run Code Online (Sandbox Code Playgroud)

或者我可能必须使用信号/插槽系统,例如Boost :: Signals,但我觉得这是一种矫枉过正(我不需要多线程支持,所有std::functions都是使用std :: bind构建的).

ava*_*kar 5

我不知道任何应用功能.但是,在C++ 0x中,您可以使用lambdas并编写以下内容.

std::for_each(c.begin(), c.end(),
    [](std::function<void ()> const & fn) { fn(); });
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用新的for循环.

for (auto const & fn: c)
    fn();
Run Code Online (Sandbox Code Playgroud)