Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is #__VA_ARGS__ supposed to generate when there are no arguments passed?

Example code:

#define FOO(...) You passed: #__VA_ARGS__
FOO(1,2,3)
FOO()

Preprocess with Visual C++ (version 14 CTP), get:

You passed: "1,2,3"
You passed:

In the last line, #__VA_ARGS__ is turned into nothingness. I would prefer it turned into "".

Is there a definitive reference for what is supposed to happen? I Googled a lot but couldn't find it.

Any suggested work-around would also be useful.

like image 699
Bryan Avatar asked Jul 01 '14 08:07

Bryan


2 Answers

Per 6.10.3.2 The # operator (C11):

Semantics

2 - [...] The character string literal corresponding to an empty argument is "". [...]

So I think MSVC is incorrect here.

I would workaround this using string literal concatenation:

#define FOO(...) You passed: "" #__VA_ARGS__
like image 118
ecatmur Avatar answered Sep 28 '22 10:09

ecatmur


The paragraph in the standard (ISO14882:2011(e)) is a little bit lengthy, but its quite clear:

16.3.2 The # operator

2 A character string literal is a string-literal with no prefix. If, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. Each occurrence of white space between the argument’s preprocessing tokens becomes a single space character in the character string literal. White space before the first preprocessing token and after the last preprocessing token comprising the argument is deleted. Otherwise, the original spelling of each preprocessing token in the argument is retained in the character string literal, except for special handling for producing the spelling of string literals and character literals: a \ character is inserted before each " and \ character of a character literal or string literal (including the delimiting " characters). If the replacement that results is not a valid character string literal, the behavior is undefined. The character string literal corresponding to an empty argument is "". The order of evaluation of # and ## operators is unspecified.

And since

16.3.1 Argument substitution

2 An identifier __VA_ARGS__ that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it.

this is the same for varags as it is for normal parameters.

like image 40
PlasmaHH Avatar answered Sep 28 '22 11:09

PlasmaHH