Concatenation of tokens in variable macros

In C, is it possible to combine each of the variable arguments in a variable macro?

Example:

MY_MACRO(A, B, C) will yield HDR_A, HDR_B, HDR_C
MY_MACRO(X, Y)    will yield HDR_X, HDR_Y

The normal operator ##has special meaning for variable macros (excluding the comma for an empty argument list). Concatenation in use __VA_ARGS__is performed only with the first token.

Example:

#define MY_MACRO(...) HDR_ ## __VA_AGRS__

MY_MACRO(X, Y)    yields HDR_X, Y

Suggestions?

+3
source share
1 answer

First, the comma rule that you mention is an extension of gcc, the C standard does not have it, and most likely will never have it, since the function can be achieved in various ways.

, , - - , , . P99 :

#define MY_PREFIX(NAME, X, I) P99_PASTE2(NAME, X)
#define MY_MACRO(...) P99_FOR(HDR_, P99_NARG(__VA_ARGS__), P00_SEQ, MY_PREFIX, __VA_ARGS__)
  • MY_PREFIX , .
  • P00_SEQ ,
  • P99_NARGS
+3

All Articles