3

Any trick to unpack a variadic macro? For example,

#define READ(...) means read the arguments one by one

READ(a, b, c) will be unpacked to read(a); read(b); read(c)

user1899020
  • 13,167
  • 21
  • 79
  • 154
  • 3
    Why are you using a macro in the first place? Why not variadic templates arguments where this can be done quite easily? – David G May 22 '14 at 20:48
  • 4
    Agreed. This is trivial with a variadic template. If macros are absolutely necessary, something like `BOOST_PP_SEQ_FOR_EACH`. – chris May 22 '14 at 20:50
  • Here read(a) is not a statement. It is a macro to define a function or class. – user1899020 May 22 '14 at 20:57

1 Answers1

2

You can achieve it with the "paired sliding args" macro technique as described here: https://codecraft.co/2014/11/25/variadic-macros-tricks

#define _EXPAND(args) args
#define READ1(a) read(a);
#define READ2(a,b) read(a); read(b);
#define READ3(a,b,c) read(a); read(b); read(c);
#define GETREAD(_1,_2,_3, READN,...) READN
#define READ(...) _EXPAND(GETREAD(__VA_ARGS__, READ3, READ2, READ1)(__VA_ARGS__))

The _EXPAND(args) is only needed in MSVC.

Paamand
  • 626
  • 1
  • 6
  • 17