fork(1) download
  1. #include <cstdio>
  2.  
  3. //number of arguments to a function macro
  4. #define $numargs_( $1, $2, $3, N, ... ) N
  5. #define $numargs( ... ) $numargs_( __VA_ARGS__, 3, 2, 1 )
  6.  
  7. #define $paste_( $0, $1 ) $0 ## $1
  8. #define $paste( $0, $1 ) $paste_( $0, $1 )
  9.  
  10. //maximum is 3 arguments
  11. //easy to get more
  12. #define $check1( $macro, $1 ) $macro( $1 );
  13. #define $check2( $macro, $1, $2 ) $check1( $macro, $1 ) $check1( $macro, $2 )
  14. #define $check3( $macro, $1, $2, $3 ) $check2( $macro, $1, $2 ) $check1( $macro, $3 )
  15.  
  16. //using $numargs and $paste to get correct macro name
  17. #define $checkall_( $macro, $check, ... ) $check( $macro, __VA_ARGS__ )
  18. #define $checkall( $macro, ... ) $checkall_( $macro, $paste( $check, $numargs(__VA_ARGS__) ), __VA_ARGS__ )
  19.  
  20. #define $mymacro( $arg ) printf("arg = %d\n", $arg)
  21.  
  22. int main() {
  23. $checkall( $mymacro, 1, 2, 3 );
  24. }
  25.  
  26.  
  27.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
arg = 1
arg = 2
arg = 3