复杂的前pression涉及逻辑AND(安培;&安培;) [英] complicated expression involving logical AND (&&)

查看:110
本文介绍了复杂的前pression涉及逻辑AND(安培;&安培;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main(void)
{
  int x,y,z;
  x=y=z=1;

  z = x && y && ++z;//is this fine?
}

最近我开始阅读关于序列点东西,但我想不出code以上样品是否是罚款或没有。我知道&放大器;&安培; 运营商引入了一个序列点,所以我不是很肯定前任pression Z = X'放大器的行为;&安培; Y'放大器;&安培; ++态。有人告诉我正确的答案。

I have lately started reading about sequence points stuffs but I cannot figure out whether the above sample of code is fine or not. I know the && operator introduces a sequence point so I am not very sure about the behavior of the expression z = x && y && ++z. Someone please tell me the correct answer.

推荐答案

在C ++ 03。

void main(void) 
{ 
  int x,y,z; 
  x=y=z=1;                                  // Seq1 at ;

  z = x && y && ++z;//is this fine?         // Seq2 at ;
} 

注意:请注意,有在运营商放大器序列点;&放大器;但这些都没有在本实施例相关的

NB: Note that there are sequence points at the operator && but then those are not relevant in this example.

精细!在一般情况下,可以是或可以是未。取决于x和y的值。在特定的情况下,不罚款。这code必须有一些潜在的名为<一个href=\"http://stackoverflow.com/questions/2235457/how-to-explain-undefined-behavior-to-know-it-all-newbies\">undefined行为。

Fine!. In general, may be or may be Not. Depends on the values of x and y. In your specific case, it is not fine. This code has the potential to have something called undefined behavior.

在z ++评估(在您的例子,因为x和y是1 ),那么标量'Z'在两个序列点之间修改不止一次在EX pression SEQ1和SEQ2(见下文)。需要注意的是赋值运算符不引入任何序列点是很重要的。

If z++ is evaluated (as in your example because x and y are 1), then the scalar variable 'z' is modified more than once in the expression between two sequence points Seq1 and Seq2 (see below). It is important to note that the assignment operator does not introduce any sequence point.

$ 5 / 4-除非另有说明,订单
  的操作数的评价
  个体经营者和
  个人SUBEX pressions
  前pressions和顺序
  副作用发生,是
  unspecified.53)的previous之间
  而下一个序列点标
  对象应具有其存储的值
  在由最多一次修改
  一个前pression的评价。
  此外,前值应为
  只访问,以确定该值
  被储存。
的这一要求
  段应满足每
  的容许排序
  SUBEX $一个完整的前pression的对$ pssions;
  否则该行为是不确定的。

$5/4- "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.53) 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."

在的C ++ 0x

将更新一次我自己理解@litb提到讨论的细节。现在,我只是打击它关闭

Will update it once I myself understand the details of the discussion referred to by @litb. For now, I am just striking it off

<击> C ++ 0x中不过,据我所知,没有任何的序列点的概念。这当然pression是罚款,不调用未定义的行为。这是因为,在Z++的效果被转让于Z的副作用之前测序

$ 1.9 / 15除非另有说明,
  个人操作数的评价
  运营商和SUBEX的pressions
  个别前pressions是
  unsequenced。 [注:在一个前pression
  被评估一次以上
  一个程序的执行过程中,
  unsequenced和indeterminately
  的测序评估其
  SUBEX pressions不需要进行
  始终以不同的评价。
  末端注]的数值计算
  操作者的操作数是
  值计算之前测序
  的操作者的结果。 如果一个
  标物体上的副作用是
  unsequenced相对于其他任何
  同一标对象上的副作用
  或一个值计算使用的值
  相同的标量的对象,该
  行为是不确定的。

$1.9/15- "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.

$ 7.8 / 9 - 运算类型(3.9.1),
  枚举类型,指针类型,
  指针成员类型(3.9.2),
  的std :: nullptr_t和CV-合格
  这些类型的版本(3.9.3)的
  统称为标量类型。

$3.9/9 - "Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_t, and cv-qualified versions of these types (3.9.3) are collectively called scalar types."

请注意,在恩pressionZ = Z ++;其中z是一个标量,其副作用的'Z'由于赋值操作符和后缀运算符++是unsequenced(之前,其他都没有被测序)。

Note that in the expression 'z = z++;' where z is a scalar variable, the side effects on 'z' due to assignment operator and postfix operator++ are unsequenced (neither of them is sequenced before the other).

感谢@Prasoon给予宝贵的投入从原来的版本改进这个帖子​​

Thanks @Prasoon for giving valuable inputs to refine this post from original version

这篇关于复杂的前pression涉及逻辑AND(安培;&安培;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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