逻辑或:不计算第二个操作数 (6.5.14.4),但编译器生成警告:违反标准?
Logical OR: the second operand is not evaluated (6.5.14.4), but the compiler generates warning: violation of the standard?
ISO/IEC 9899:202x (E) 工作草案 — 2020 年 2 月 5 日 C17..C2x N2479:
6.5.14 Logical OR operator:
- If the first operand compares unequal to 0, the second operand is not evaluated.
上下文:有一个 C 编译器,它会为涉及逻辑运算符的表达式生成警告 condition is always true / false
。例如,如果表达式a || b
中的变量b
是unequal to 0
,那么编译器会生成condition is always true
(提到b
在源代码中的位置)。
问题:生成此类警告的事实是否可以解释为违反 6.5.14.4
?请提供解释/论证/参考资料。
注意:gcc / clang / cl
(配置为最高警告级别)不会为上述示例生成任何警告。
更新。 MRE:
int main(void)
{
int c1 = 1, c2 = 1, r = 0;
if ( c1 || c2 ) { r = 1; }
return r;
}
$ cc x.c
x.c:4:10: warning: condition is always true
x.c:4:16: warning: condition is always true
虽然有人可能会争辩说编译器知道 ||
的第二个操作数的值的唯一方法是对其求值,这违反了 C 2018 6.5.14 4 ( “...如果 ||
的第一个操作数比较不等于 0,则不评估第二个操作数”)如果第一个操作数比较不等于 0,可以理解“评估”是指在程序执行期间进行的评估,不是翻译。 C 2018 5.1 中描述的 C 概念模型将翻译和执行分开。 6.5.14 4 是禁止在程序执行期间求值,而不是在翻译期间。
ISO/IEC 9899:202x (E) 工作草案 — 2020 年 2 月 5 日 C17..C2x N2479:
6.5.14 Logical OR operator:
- If the first operand compares unequal to 0, the second operand is not evaluated.
上下文:有一个 C 编译器,它会为涉及逻辑运算符的表达式生成警告 condition is always true / false
。例如,如果表达式a || b
中的变量b
是unequal to 0
,那么编译器会生成condition is always true
(提到b
在源代码中的位置)。
问题:生成此类警告的事实是否可以解释为违反 6.5.14.4
?请提供解释/论证/参考资料。
注意:gcc / clang / cl
(配置为最高警告级别)不会为上述示例生成任何警告。
更新。 MRE:
int main(void)
{
int c1 = 1, c2 = 1, r = 0;
if ( c1 || c2 ) { r = 1; }
return r;
}
$ cc x.c
x.c:4:10: warning: condition is always true
x.c:4:16: warning: condition is always true
虽然有人可能会争辩说编译器知道 ||
的第二个操作数的值的唯一方法是对其求值,这违反了 C 2018 6.5.14 4 ( “...如果 ||
的第一个操作数比较不等于 0,则不评估第二个操作数”)如果第一个操作数比较不等于 0,可以理解“评估”是指在程序执行期间进行的评估,不是翻译。 C 2018 5.1 中描述的 C 概念模型将翻译和执行分开。 6.5.14 4 是禁止在程序执行期间求值,而不是在翻译期间。