fork download
  1. #define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
  2. #define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
  3. #define CONCATENATE2(arg1, arg2) arg1##arg2
  4.  
  5. #define FOR_EACH_1(what, arg, x) what(arg, x)
  6. #define FOR_EACH_2(what, arg, x, ...) what(arg, x) FOR_EACH_1(what, arg, __VA_ARGS__);
  7. #define FOR_EACH_3(what, arg, x, ...) what(arg, x) FOR_EACH_2(what, arg, __VA_ARGS__);
  8. #define FOR_EACH_4(what, arg, x, ...) what(arg, x) FOR_EACH_3(what, arg, __VA_ARGS__);
  9. #define FOR_EACH_5(what, arg, x, ...) what(arg, x) FOR_EACH_4(what, arg, __VA_ARGS__);
  10. #define FOR_EACH_6(what, arg, x, ...) what(arg, x) FOR_EACH_5(what, arg, __VA_ARGS__);
  11. #define FOR_EACH_7(what, arg, x, ...) what(arg, x) FOR_EACH_6(what, arg, __VA_ARGS__);
  12. #define FOR_EACH_8(what, arg, x, ...) what(arg, x) FOR_EACH_7(what, arg, __VA_ARGS__);
  13.  
  14. #define FOR_EACH_NARG(...) FOR_EACH_NARG_(__VA_ARGS__, FOR_EACH_RSEQ_N())
  15. #define FOR_EACH_NARG_(...) FOR_EACH_ARG_N(__VA_ARGS__)
  16. #define FOR_EACH_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
  17. #define FOR_EACH_RSEQ_N() 8, 7, 6, 5, 4, 3, 2, 1, 0
  18.  
  19. #define FOR_EACH_(N, what, arg, x, ...) CONCATENATE(FOR_EACH_, N)(what, arg, x, __VA_ARGS__)
  20. #define FOR_EACH(what, arg, x, ...) FOR_EACH_(FOR_EACH_NARG(x, __VA_ARGS__), what, arg, x, __VA_ARGS__)
  21.  
  22. #define DO_WITH(var,action) var.action;
  23. #define WITH(var, ...) ([&]{ FOR_EACH(DO_WITH, var, __VA_ARGS__) }())
  24.  
  25. #include <cstdio>
  26. #include <vector>
  27.  
  28. int main()
  29. {
  30. std::vector<int> v;
  31. WITH(v,
  32. push_back(1),
  33. push_back(2),
  34. resize(5,3));
  35. for (int i : v) printf("%d\n", i);
  36. }
  37.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
1
2
3
3
3