Boost logo

Boost :

Subject: Re: [boost] Review Request: Variadic Macro Data library
From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2011-02-21 13:03:04


On Mon, 21 Feb 2011 12:12:15 -0500, Lorenzo Caminiti wrote:

> On Mon, Feb 21, 2011 at 9:19 AM, Edward Diener <eldiener_at_[hidden]>
> wrote:

>>> However, DATA_SIZE() =>  0 is ill-conceived.  An empty argument is
>>> still an argument to the preprocessor.  A better correlation is:
>>>
>>> DATA_SIZE(,,) =>  3
>>> DATA_SIZE(,) =>  2
>>> DATA_SIZE() =>  1
>>
>> Thanks ! I will just have to further document that the data size
>> returned can never be 0, even when the variadic macro is invoked with
>> an empty argument.
>
> IMO, that makes sense -- so documentation is a good option.
>
> However, I still don't understand why MSVC accepts this DATA_SIZE()
> invocation:
>
> #define DATA_SIZE(...)
> DATA_SIZE(1)
> DATA_SIZE() // No error -- why??
>
> #define SIZE(x)
> SIZE(1)
> SIZE() // Error -- as it should!

Neither case should be an error. According to the C99 standard (and C+
+0x), the sequence of tokens (and whitespace separations) that makes up a
macro argument may be empty or contain no tokens. If the sequence of
tokens (and whitespace separations) contains no tokens, the formal
parameters in the replacement list are replaced by a "placemarker" which
is a sort of virtual token.

So, in the following scenarios:

#define MACRO(x) [x]

MACRO(123) // [123]
MACRO( 123 ) // [ 123 ]
MACRO( 123 ) // [ 123 ]
        // adjacent whitespace is combined
        // in an earlier phase of translation

MACRO( ) // [<placemarker>] -> []
MACRO() // [<placemarker>] -> []

For variadics, for the purposes of substitution, token-pasting, and
stringizing, all of the variadic arguments act as one argument:

#define MACRO(...) [__VA_ARGS__]

MACRO() // [<placemarker>] -> []
MACRO( ) // [<placemarker>] -> []
MACRO(a) // [a]
MACRO(a,b) // [a,b]
MACRO(a, b) // [a, b]
MACRO( a,b, c ) // [ a,b, c ]

* Note that I don't know of a single preprocessor that actually handles
whitespace correctly in all cases.

Regards,
Paul Mensonides


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk