Note that there are some explanatory texts on larger screens.

plurals
  1. POMSVC++ variadic macro expansion
    primarykey
    data
    text
    <p>So I've got a macro that works nicely in GCC, but not in Microsoft's C++ Compiler. I'm hoping somebody might know of a workaround, or perhaps can explain to me why it behaves this way. </p> <p>I'm sure this macro isn't exactly "standard", but it would really help me out.</p> <p>Here is a functional example of the macro:</p> <pre><code>#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N #define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1) #define FULLY_EXPANDED(count, ...) \ MAC ## count (__VA_ARGS__) #define SEMI_EXPANDED(count, ...) FULLY_EXPANDED(count, __VA_ARGS__) #define EXPAND_THESE(...) SEMI_EXPANDED(VA_NARGS(__VA_ARGS__), __VA_ARGS__) #define ACTUAL_MACRO(x) parent-&gt;GetProperty&lt;x&gt;(); #define MAC1(a) ACTUAL_MACRO(a) #define MAC2(a,b) MAC1(a) ACTUAL_MACRO(b) #define MAC3(a,b,c) MAC2(a,b) ACTUAL_MACRO(c) #define MAC4(a,b,c,d) MAC3(a,b,c) ACTUAL_MACRO(d) #define MAC5(a,b,c,d,e) MAC4(a,b,c,d) ACTUAL_MACRO(e) </code></pre> <p>Here is how I might use this macro:</p> <pre><code>struct MyStructure { void Foo() { EXPAND_THESE(Property1, Property2, Property3, Property4) } Base * parent; } </code></pre> <p>Here's how GCC expands the above:</p> <pre><code>struct MyStructure { void Foo() { parent-&gt;GetProperty&lt;Property1&gt;(); parent-&gt;GetProperty&lt;Property2&gt;(); parent-&gt;GetProperty&lt;Property3&gt;(); parent-&gt;GetProperty&lt;Property4&gt;(); } Base * parent; } </code></pre> <p>But Microsoft for some reason expands all my __VA_ARGS__ as one argument:</p> <pre><code>struct MyStructure { void Foo() { parent-&gt;GetProperty&lt;Property1, Property2, Property3, Property4&gt;(); } Base * parent; } </code></pre> <p>Does anybody know why this is? Is there some trick I can pull to get Microsoft to expand this like GCC? Maybe toss in a couple extra pairs of parentheses?</p> <p>Macros like this could really help me out in replacing a bunch of "glue" code, but because of this problem, I can't move it into my VS project. Any help would be greatly appreciated!</p> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload