fork(4) download
  1. #include <stdio.h>
  2.  
  3. template <typename T, typename FT, FT &F>
  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. int y = 2;
  13. auto f = [=](int &x) { x = x * y; };
  14.  
  15. int main()
  16. {
  17. int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  18. myforeach<int, decltype(f), f>(a, 10);
  19. for(int i = 0; i < 10; i++)
  20. {
  21. printf("%d ", a[i]);
  22. }
  23. printf("\n");
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0 2 4 6 8 10 12 14 16 18