Call all functors in a container

I have a sequence of std :: function objects (a very primitive form of a signaling system). Is there a standard (in C ++ 0x) function or functor that will call this std :: function? I'm currently using

std::for_each(c.begin(), c.end(), 
              std::mem_fn(&std::function<void ()>::operator()));

IMHO this std::mem_fn(&std::function<void ()>::operator())is ugly. I would like to write

std::for_each(c.begin(), c.end(), funcall);

Is there such a thing funcall? Alternatively I can implement a function

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

Or I need to use a signal / slot system like Boost :: Signals, but I have the feeling that it is overkill (I don't need multithreading support here, everything is std::functionbuilt using std ::. Link)

+3
source share
1 answer

- . ++ 0x lambdas .

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

for.

for (auto const & fn: c)
    fn();
+5

Source: https://habr.com/ru/post/1780659/


All Articles