fork 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(_) static_assert(false, "Not enough argument")
  11. #define F_2(a, s) std::string a = s;
  12. #define F_3(a, b, s) std::string a ## _ ## b = s;
  13. #define F_4(a, b, c, s) std::string a ## _ ## b ## _ ## c= s;
  14. #define F_5(a, b, c, d, s) std::string a ## _ ## b ## _ ## c ## _ ## d= s;
  15. #define F_6(a, b, c, d, e, s) std::string a ## _ ## b ## _ ## c ## _ ## d ## _ ## e = s;
  16. #define F_7(a, b, c, d, e, f, s) std::string a ## _ ## b ## _ ## c ## _ ## d ## _ ## e ## _ ## f= s;
  17. #define F_8(a, b, c, d, e, f, g, s) std::string a ## _ ## b ## _ ## c ## _ ## d ## _ ## e ## _ ## f ## _ ## g = s;
  18.  
  19. #define DISPATCH(N) F_ ## N
  20.  
  21. #define Macro(...) IDENTITY(APPLY(DISPATCH, COUNT(__VA_ARGS__)))(__VA_ARGS__)
  22.  
  23. int main() {
  24. Macro(one, name, "hello ")
  25. Macro(one, two, name, "world\n")
  26.  
  27. std:: cout << one_name << one_two_name;
  28. }
  29.  
  30.  
Success #stdin #stdout 0s 4264KB
stdin
Standard input is empty
stdout
hello world