是`C == C ++`未定义的行为? [英] Is `C == C++` undefined behaviour?

查看:172
本文介绍了是`C == C ++`未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位朋友告诉我,之后:

A friend tells me that after:

int C = anything;

C == C ++ true 。这是一个笑话,一个反驳的种类到所谓的C不等于C ++。

C == C++ will have the value true. This is intended as a joke, a rebuttal of sorts to the oft-claimed "C is not the same as C++".

但是,因为 == 不是一个序列点,我认为这实际上是未定义的行为。该程序可以首先评估 C ++ ,使得 C> C ++ C == C ++ 都未定义。但是, C> = C ++ 将始终评估为true。当然,当操作数被翻转时( C ++ <= C 总是为真,其他都未定义),同样的情况也是如此。

However, since == is not a sequence point, I argue that this is in fact undefined behavior. The program may first evaluate C++, so that C > C++ and C == C++ are both undefined. However, C >= C++ will always evaluate as true. The same, of course, is true when the operands are flipped (C++ <= C is always true and everything else is undefined).

此分析是否正确?

推荐答案

所有情况都会导致未定义的行为和不可预测的结果。

All cases lead to undefined behavior and unpredictable results.

草案C ++ 11标准告诉我们,除非另有说明,否则操作数的计算顺序是无序的,如果相同的标量对象被修改更多一次通过unseqeucend副作用比我们有未定义的行为。如果我们需要修改对象,并且必须为另一个操作数计算对象的值,它也是未定义的。这在C ++ 11标准部分 1.9

The draft C++11 standard tells us that unless stated otherwise the order of evaluations of operands are unsequenced and if the same scalar object is modified more that once by unseqeucend side effects than we have undefined behavior. It is also undefined if we need modify the object and have to compute the value of the object for another operand. This is covered in the draft C++11 standard section 1.9


注意,单个算子的操作数
和单个表达式的子表达式的评估是不可靠的。 [
注意:在一个程序的
执行过程中多次评估的表达式中,不需要对其子表达式的未定序和不确定排序的
求值一致地执行
评价。 -end note]运算符的
操作数的值计算在运算符的
的值计算之前排序。如果对标量对象的副作用是
相对于对同一标量
对象的另一个副作用或使用相同标量
对象的值的计算,该行为是未定义的。

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. —end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.

5.9 中的等式运算符或关系运算符关系运算符 5.10 等于运算符指定操作数的排序。

Neither the equality operators or relational operators in sections 5.9 Relational operators and 5.10 Equality operators specify a sequencing for the operands.

clang 也为这种情况提供警告,默认情况下,它应该类似于以下内容(查看活动

clang also provides a warning for this case, it looks like by default, it should something similar to the following (see it live):

 warning: unsequenced modification and access to 'C' [-Wunsequenced]
  if( C == C++ )
      ~     ^

这也是C ++ 03中未定义的行为,没有使用排序关系的概念,只是序列点。在草案C ++ 03标准中,相关部分将为第5章 表达式,其中:

This is also undefined behavior in C++03 with did not use the concept of sequencing relationships but just sequence points. In the draft C++03 standard the relevant section would be Chapter 5 Expressions which says:


除非另有说明,单个
运算符的操作数和单个表达式的子表达式的操作数的评估顺序以及发生副作用的顺序
未指定.57)在
上一个和下一个序列点之间,标量对象必须通过表达式的求值修改其存储的
值一次。
此外,只能访问先前值以确定要存储的
值。本段的要求对于满
表达式的子表达式的每个可允许排序应满足
;

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.57) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

这是更简单的推理,因为多次修改或修改和使用标量的值在同一个序列点内是不确定的行为,而不必弄清楚操作的顺序。

which is simpler to reason about since multiple modification of or a modification and use of the value of a scalar within the same sequence point is undefined behavior without having to figure out the sequencing of operations.

这篇关于是`C == C ++`未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆