fork download
  1. #include <iostream>
  2.  
  3. template < unsigned N > constexpr
  4. unsigned countarg( const char( &s )[N], unsigned i = 0, unsigned c = 0 )
  5. {
  6. return
  7. s[i] == '\0'
  8. ? i == 0
  9. ? 0
  10. : c + 1
  11. : s[i] == ','
  12. ? countarg( s, i + 1, c + 1 )
  13. : countarg( s, i + 1, c );
  14. }
  15.  
  16. constexpr
  17. unsigned countarg()
  18. {
  19. return 0;
  20. }
  21.  
  22. #define ARGC( ... ) countarg( #__VA_ARGS__ )
  23.  
  24.  
  25. int main()
  26. {
  27. std::cout
  28. << ARGC() << std::endl
  29. << ARGC( 1 ) << std::endl
  30. << ARGC( one, two ) << std::endl
  31. << ARGC( "abc", 123, XYZ ) << std::endl
  32. << ARGC( unknown = 0, red = 1, green = 2, blue = 4 ) << std::endl
  33. << ARGC( "1", "2", "3", "4", "5" ) << std::endl
  34. << "Wrong (comma must be escaped): " << ARGC( "This is a comma: ," ) << std::endl
  35. << "Fine: " << ARGC( "This is a comma: \x2c" );
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
0
1
2
3
4
5
Wrong (comma must be escaped): 2
Fine: 1