fork(1) download
  1. #include <iostream>
  2.  
  3. #define COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
  4. #define COUNT(...) COUNT_N(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
  5. // Warning: COUNT() return 1 (as COUNT(A)) :-/
  6.  
  7. #define IDENTITY(N) N
  8. #define APPLY(macro, ...) IDENTITY(macro(__VA_ARGS__))
  9.  
  10. #define F_1(a) functionBeingUsed(); a;
  11. #define F_2(a, b) functionBeingUsed(a); b;
  12. #define F_3(a, b, c) functionBeingUsed(a, b); c;
  13. #define F_4(a, b, c, d) functionBeingUsed(a, b, c); d;
  14. #define F_5(a, b, c, d, e) functionBeingUsed(a, b, c, d); e;
  15. #define F_6(a, b, c, d, e, f) functionBeingUsed(a, b, c, d, e); f;
  16. #define F_7(a, b, c, d, e, f, g) functionBeingUsed(a, b, c, d, e, f); g;
  17. #define F_8(a, b, c, d, e, f, g, h) functionBeingUsed(a, b, c, d, e, f, g); h;
  18.  
  19. #define DISPATCH(N) F_ ## N
  20.  
  21. #define Macro(...) IDENTITY(APPLY(DISPATCH, COUNT(__VA_ARGS__)))(__VA_ARGS__)
  22.  
  23. void functionBeingUsed(int a, int b, int c) { std::cout << "hello world 3\n"; }
  24. void functionBeingUsed(int a, int b, int c, int d) { std::cout << "hello world 4\n"; }
  25.  
  26. int main() {
  27. Macro(1,2,3,4, int i = 0);
  28. Macro(1,2,3, int j = 0);
  29. }
  30.  
  31.  
Success #stdin #stdout 0s 4360KB
stdin
Standard input is empty
stdout
hello world 4
hello world 3