0

I'm experimenting with macro replacement for functions.

LIVE EXAMPLE

Here is the simple vararg macro definition:

#define FOO(a, ...) printf(a, ##__VA_ARGS__)

So when we define

FOO("1"); //preprocesses to printf("1"), ',' is truncated

FOO("1", 2, 3); //preprocesses to printf("1", 2, 3), ',' is left unchanged

Without ## preceding __VA_ARGS__ the ',' does not truncated. So I expected that ## operator does the job, but its definition in the standard is a bit blurred. Here are some quotes:

6.10.3.3(p2):

if an argument consists of no preprocessing tokens, the parameter is replaced by a placemarker preprocessing token instead

6.10.3.3(p3):

For both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token.

From the 2 sections I don't see any reason that in the invokation FOO("1") expands to printf("1"), not printf(1,). __VA_ARGS__ consits of no preprocessing tokens.

So if we define

#define FOO(a, b) printf(a, ##b)

Why does FOO(1,) expand to printf(1,)? b also consits of no preprocessing tokens here.

Some Name
  • 8,555
  • 5
  • 27
  • 77

0 Answers0