c++ - Is it valid to use boolean literals in preprocessor conditionals? -
consider following code, results in boolean literal true
being evaluated in preprocessor conditional:
#define some_macro true int main () { #if some_macro return 1; #else return 0; #endif }
clang 3.4 , gcc 4.8 both accept code, -pedantic -std=c++11 -wall -wextra
.
visual studio 2013 rejects it, fatal error c1017: invalid integer constant expression.
my reading of n3376 § 16.1 regular c++ rules evaluating constant expressions should apply.
if so, code valid, , it's bug if msvc not accept it.
but don't find standardeze particularly clear. confirm this?
yes, valid. see c++11 §16.1/4 (emphasis mine)
prior evaluation, macro invocations in list of preprocessing tokens become controlling constant expression replaced (except macro names modified
defined
unary operator), in normal text. if tokendefined
generated result of replacement process or use ofdefined
unary operator not match 1 of 2 specified forms prior macro replacement, behavior undefined. after replacements due macro expansion ,defined
unary operator have been performed, all remaining identifiers , keywords, excepttrue
,false
, replaced pp-number0
, , each preprocessing token converted token. resulting tokens comprise controlling constant expression evaluated according rules of 5.19 using arithmetic has @ least ranges specified in 18.3. purposes of token conversion , evaluation signed , unsigned integer types act if have same representation as, respectively,intmax_t
oruintmax_t
(18.4). includes interpreting character literals, may involve converting escape sequences execution character set members. whether numeric value these character literals matches value obtained when identical character literal occurs in expression (other within#if
or#elif
directive) implementation-defined. also, whether single-character character literal may have negative value implementation-defined. each subexpression typebool
subjected integral promotion before processing continues.
Comments
Post a Comment