title: C++ macro unpack and join
body: <p>I am trying to define a C++ struct with variable members by a macro as part of an introspection metadata system. Given that I am creating a struct definition I cannot use compile-time templates (right?). Instead I create a variadic macro that should accept tuples of (type, name) but I have had problems in writing a macro that would expand and join the tuple with a symbol to achieve something like</p>
  
  <p><code>JOIN(.,(a,A)) -&gt; a.A</code>
  or
  <code>JOIN(&amp;,(b,B)) -&gt; b&amp;B</code>.</p>
  
  <p>I am working in Visual Studio 2017 and part of my confusion may be the inconsistency in the <strong>VA_ARGS</strong> expansion in MSVC and GCC:
  <a href="https://stackoverflow.com/questions/9183993/msvc-variadic-macro-expansion">MSVC++ variadic macro expansion</a></p>
  
  <p>My approach has been to write an unpacking macro that simply strips the parenthesis from the tuple and a join macro that would join the arguments by the desired string:</p>
  
  <pre><code>#define UNPACK(a, b) a, b
  #define JOINAB(a, b, S) a S b
  #define JOINTUPLE(S, ab) JOINAB(UNPACK ab, S)
  </code></pre>
  
  <p>But this does not work as it seems the macro is not evaluated in the right order. I have then tried to explicitly expand the arguments, e.g. <code>#define EXPAND(args) args</code>, but to no luck.</p>
  
  <p>I have finally found a workaround by embedding the argument parenthesis in the unpacking, thereby 'forcing' an order of evaluation:</p>
  
  <pre><code>#define EXPAND(...) __VA_ARGS__
  #define UNPACK(a, b) (a, b
  #define JOINAB(a, b, S) a S b
  #define JOINTUPLE(S, ab) EXPAND(JOINAB UNPACK ab, S))
  </code></pre>
  
  <p>which works, but seems extremely hacky...</p>
  
  <p>My questions are</p>
  
  <ol>
  <li>Is there a proper way to achieve the evaluation of the unpacked result?</li>
  <li>Why do I need the EXPAND? Without it the expression <code>JOINTUPLE(:,(a,B))</code> resolves to <code>JOIN (a, B, :)</code> but why is this not further processed to <code>a : B</code>?</li>
  <li>Could there be a way to solve it with token-pasting operator? <code>S</code> would only exist in 3 variants.</li>
  </ol>