fork download
  1. #include <stdio.h>
  2.  
  3. template <typename T, void(*F)(T&)>
  4. void myforeach(T *p, int N)
  5. {
  6. for(int i = 0; i < N; i++)
  7. {
  8. F(p[i]);
  9. }
  10. }
  11.  
  12. template <int Y>
  13. void f(int &x)
  14. {
  15. x = x * Y;
  16. }
  17.  
  18. int main()
  19. {
  20. int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  21. myforeach<int, f<2>>(a, 10);
  22. for(int i = 0; i < 10; i++)
  23. {
  24. printf("%d ", a[i]);
  25. }
  26. printf("\n");
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
0 2 4 6 8 10 12 14 16 18