Advertisement
Avidanborisov

Visual Studio VA_NUM_ARGS

Mar 12th, 2013
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.69 KB | None | 0 0
  1. #define VA_NUM_ARGS(...) VA_ARGS_EXIST(__VA_ARGS__) ? VA_NUM_ARGS_(__VA_ARGS__) : 0
  2. #define VA_NUM_ARGS_(...) (VA_LAST_ARG((__VA_ARGS__, VA_CREATE_ARGS())))
  3. #define VA_ARGS_EXIST(...) sizeof(VA_STRINGIFY(VA_APPEND(__VA_ARGS__))) != 2 * sizeof(char)
  4. #define VA_LAST_ARG(tuple) VA_LAST_ARG_ tuple
  5. #define VA_LAST_ARG_(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
  6. #define VA_CREATE_ARGS() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  7. #define VA_STRINGIFY(...) VA_STRINGIFY_(__VA_ARGS__)
  8. #define VA_STRINGIFY_(...) #__VA_ARGS__
  9. #define VA_APPEND(...) __VA_ARGS__##1
  10.  
  11. int foo = VA_NUM_ARGS();       // -> 0
  12. int bar = VA_NUM_ARGS(1);      // -> 1
  13. int baz = VA_NUM_ARGS(1, 2, 3) // -> 3
  14.  
  15. /* Yep, all works! */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement