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 STRINGIFY_1(a) #a
  11. #define STRINGIFY_2(a, b) #a, #b
  12. #define STRINGIFY_3(a, b, c) #a, #b, #c
  13. #define STRINGIFY_4(a, b, c, d) #a, #b, #c, #d
  14. #define STRINGIFY_5(a, b, c, d, e) #a, #b, #c, #d, #e
  15. #define STRINGIFY_6(a, b, c, d, e, f) #a, #b, #c, #d, #e, #f
  16. #define STRINGIFY_7(a, b, c, d, e, f, g) #a, #b, #c, #d, #e, #f, #g
  17. #define STRINGIFY_8(a, b, c, d, e, f, g, h) #a, #b, #c, #d, #e, #f, #g, #h
  18.  
  19. #define DISPATCH(N) STRINGIFY_ ## N
  20.  
  21. #define STRINGIFY(...) IDENTITY(APPLY(DISPATCH, COUNT(__VA_ARGS__)))(__VA_ARGS__)
  22.  
  23.  
  24. int main() {
  25. const char* words[] = { STRINGIFY(42, 5, toto, hello) };
  26.  
  27. for (const auto s : words) {
  28. std::cout << s << std::endl;
  29. }
  30. }
  31.  
Success #stdin #stdout 0s 4392KB
stdin
Standard input is empty
stdout
42
5
toto
hello