Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Surprising expansion of variadic GNU C preprocessor macros in the presence of the ## operator [duplicate]

$
0
0

If we define a macro

#define M(x, ...) { x, __VA_ARGS__ }

and then use it passing itself as an argument

M(M(1, 2), M(3, 4), M(5, 6))

then it expands to the expected form:

{ { 1, 2 }, { 3, 4 }, { 5, 6 } }

However, when we use the ## operator (to prevent dangling comma from appearing in the output in the case of the single argument invocations, as documented in the GCC manual), i.e.

#define M0(x, ...) { x, ## __VA_ARGS__ }

then the expansion of arguments in

M0(M0(1,2), M0(3,4), M0(5,6))

seems to stop after the first argument, i.e. we get:

{ { 1,2 }, M0(3,4), M0(5,6) }

Is this behavior a bug, or does it stem from some principle?

(I have also checked it with clang, and it behaves in the same way as GCC)


Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>