c++ - 有条件包含 : integral constant expression is unlimited?

标签 c++ floating-point language-lawyer constant-expression

根据 C++11(及更新版本),此代码有效:

#if 1.0 > 2.0 ? 1 : 0
#endif

但是,大多数(如果不是全部)C++ 编译器都拒绝它:

$ 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 有这个(强调):

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.

还有这个(强调):

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.

1.0 > 2.0 ? 1 : 0 是整型常量表达式。

那么,C++ 标准在哪里禁止在控制条件包含的表达式中使用浮点文字(例如)?

最佳答案

理查德·史密斯的回答:

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).

关于c++ - 有条件包含 : integral constant expression is unlimited?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70943108/

相关文章:

c++ - 存在用户定义的移动构造函数/赋值时的隐式复制构造函数

c++ - C++98 标准在哪里指定对静态成员的调用何时依赖于模板?

c++ - double 的有效小数位数?

c++ - 从有符号字符转换为无符号字符然后再转换回来?

c++ - Sublime Text 3 在 OS X 上找到默认构建系统

c++ - C 或 C++ 中的十进制算术?

python - 尝试将对象转换为python中的int类型的问题

c++ - 拆分句子并检索特定单词

c++ - 具有多边形裁剪区域的 Liang Barsky 裁线算法

mysql - 在 MySQL 中使用 float 类型更新