简体   繁体   中英

Conditional inclusion: integral constant expression is unlimited?

Per C++11 (and newer) this code is valid:

#if 1.0 > 2.0 ? 1 : 0
#endif

However, most (if not all) C++ compilers reject it:

$ echo "#if 1.0 > 2.0 ? 1 : 0" | g++ -xc++ - -std=c++11 -pedantic -c
<stdin>:1:5: error: floating constant in preprocessor expression
<stdin>:1:11: error: floating constant in preprocessor expression

N4849 has this (emphasis added):

The expression that controls conditional inclusion shall be an integral constant expression except that identifiers (including those lexically identical to keywords) are interpreted as described below143 and it may contain zero or more defined-macro-expressions and/or has-include-expressions and/or has-attribute-expressions as unary operator expressions.

and this (emphasis added):

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.

The 1.0 > 2.0? 1: 0 1.0 > 2.0? 1: 0 is integral constant expression.

So, where C++ standard prohibits using floating-point literal in the expression that controls conditional inclusion?

Answer from Richard Smith:

This is an error in the standard wording. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1436 for details and a proposed fix -- though that fix is known to be wrong too (it permits lambda-expressions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM