Boost logo

Boost :

Subject: Re: [boost] [preprocessor] check if a token is a keyword (was "BOOST_PP_IS_UNARY()")
From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2010-08-16 03:24:02


On 8/13/2010 7:52 AM, Lorenzo Caminiti wrote:
> Hello all,
>
> I asked this question before but received no answer: Is there any
> problem in using `BOOST_PP_UNARY()` to check if a token matches a
> predefined keyword as indicated by the code below?
>
> The following `IS_PUBLIC(token)` macro expands to 1 if `token` is
> `public`, to 0 otherwise:
>
> #define PUBLIC_public (1)
> #define IS_PUBLIC(token) BOOST_PP_IS_UNARY(BOOST_PP_CAT(PUBLIC_, token))
>
> IS_PUBLIC(public) // Expand to 1.
> IS_PUBLIC(abc) // Expand to 0.
>
> This works just fine on both GCC and MSVC. However, `BOOST_PP_UNARY()`
> is only part of Boost.Preprocessor private API because:

IS_PUBLIC(+) results in undefined behavior. More generally, the result
of token-pasting must result in a single (preprocessing) token. So, for
example, BOOST_PP_CAT(abc, 123) and BOOST_PP_CAT(+, +) are okay, but
BOOST_PP_CAT(abc, 123.0) and BOOST_PP_CAT(+, -) are not. Similarly for
what you have above which will work provided everything used as an
argument is an identifier (there are no "keywords" to the preprocessor)
or a pp-number that doesn't contain any decimal points.

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