fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4.  
  5. template<typename Arg>
  6. auto func(Arg arg)
  7. -> decltype(arg(), void())
  8. {
  9. arg();
  10. }
  11.  
  12. template<typename Container>
  13. auto func(Container& c)
  14. -> decltype(c.rbegin() != c.rend(), (*c.rbegin())(), void())
  15. {
  16. for (auto it = c.rbegin(); it != c.rend(); ++it)
  17. {
  18. (*it)();
  19. }
  20. }
  21.  
  22. template <typename ...Ts>
  23. void funcs(Ts&&... args)
  24. {
  25. const int dummy[] = {(func(std::forward<Ts>(args)), 0)..., 0};
  26. static_cast<void>(dummy); // Avoid warning for unused variable
  27.  
  28. // Or in C++17:
  29. // (func(std::forward<Ts>(args)), ...);
  30. }
  31.  
  32. void f()
  33. {
  34. std::cout << "+" ;
  35. }
  36.  
  37. int main()
  38. {
  39. funcs(f,f,f); // #1
  40.  
  41. std::cout << std::endl;
  42.  
  43. std::vector<std::function<void()> > v{f,f};
  44. funcs(v, f, v); // #2
  45. }
  46.  
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
+++
+++++