fork download
  1. #include <iostream>
  2.  
  3. struct test1
  4. {
  5. void operator()(int i)
  6. {
  7. std::cout << "[1] " << i << std::endl;
  8. }
  9. };
  10.  
  11. void test2(int i)
  12. {
  13. std::cout << "[2] " << i << std::endl;
  14. }
  15.  
  16. void test3_impl(int i)
  17. {
  18. std::cout << "[3] " << i << std::endl;
  19. }
  20. void (*test3)(int) = test3_impl;
  21.  
  22. template <typename Function>
  23. void call_each(int begin, int end, Function f)
  24. {
  25. for (int i=begin; i<end; ++i)
  26. f(i);
  27. }
  28.  
  29. int main() {
  30. call_each(1, 4, test1());
  31. call_each(2, 7, test2);
  32. call_each(7, 10, test3);
  33. //call_each(10, 13, [](int i) -> void { std::cout << "[4] " << i << std::endl; });
  34. }
  35.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
[1] 1
[1] 2
[1] 3
[2] 2
[2] 3
[2] 4
[2] 5
[2] 6
[3] 7
[3] 8
[3] 9