Consider the following code, which results in the boolean literal true being evaluated in a preprocessor conditional:
#define SOME_MACRO true
int main ()
{
#if SOME_MACRO
return 1;
#else
return 0;
#endif
}
Clang 3.4 and GCC 4.8 both accept this code, even with -pedantic -std=c++11 -Wall -Wextra.
Visual Studio 2013 rejects it, with fatal error C1017: invalid integer constant expression.
My reading of n3376 § 16.1 is that the regular C++ rules for evaluating constant expressions should apply.
If so, this code is valid, and it's a bug if MSVC does not accept it.
But I don't find the standardeze particularly clear. Could someone confirm this?
Yes, it is valid. See C++11 §16.1/4 (emphasis mine)
Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the
definedunary operator), just as in normal text. If the tokendefinedis generated as a result of this replacement process or use of thedefinedunary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined. After all replacements due to macro expansion and thedefinedunary operator have been performed, all remaining identifiers and keywords, except fortrueandfalse, are replaced with the pp-number0, and then each preprocessing token is converted into a token. The resulting tokens comprise the controlling constant expression which is evaluated according to the rules of 5.19 using arithmetic that has at least the ranges specified in 18.3. For the purposes of this token conversion and evaluation all signed and unsigned integer types act as if they have the same representation as, respectively,intmax_toruintmax_t(18.4). This includes interpreting character literals, which may involve converting escape sequences into execution character set members. Whether the numeric value for these character literals matches the value obtained when an identical character literal occurs in an expression (other than within a#ifor#elifdirective) is implementation-defined. Also, whether a single-character character literal may have a negative value is implementation-defined. Each subexpression with typeboolis subjected to integral promotion before processing continues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With