涉及逻辑AND(& amp;)的复杂表达 [英] complicated expression involving logical AND (&&)

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

问题描述

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

z = x&&& y&& ++ z; //这是罚款吗?
}



我最近开始阅读关于序列点的东西,但我不知道上面的代码示例是否正确。我知道&& 运算符引入了一个序列点,所以我不太确定表达式z = x&& y&& ++ z。有人请告诉我正确的答案。

解决方案

在C ++ 03。

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

z = x&& y&& ++ z; //这是罚款吗? // Seq2 at;
}



NB:注意在操作员&但这些在这个例子中不相关。



很好!一般来说,可以是或可以不是。取决于x和y的值。在你的具体情况下,这不是罚款。此代码有可能会有一些名为未定义的行为



如果对z ++进行求值(如示例中的,因为x和y为1 ),在两个序列点Seq1和Seq2(见下文)之间的表达中被不止一次地修饰。重要的是注意赋值运算符不引入任何序列点。


$ 5 / 4- b $ b对
个体运算符的操作数的评估以及
个别
表达式的子表达式,以及
副作用发生的顺序是
未指定。 )在前一个
和下一个序列点之间,标量
对象的存储值
最多只能通过表达式的
求值修改
此外,先前值应为
,仅用于确定要存储的值
对于每个
,必须满足该
段的要求完整表达式的
子表达式;
否则行为未定义。


在C ++ 0x



一旦我自己了解@litb提及的讨论细节,就会更新。现在,我只是醒来了



在C ++ 0X中,但是,据我所知,没有序列点的概念。这个表达式很好,不会调用未定义的行为。这是因为++对'z'的效果在赋值给z的副作用之前排序。


$ 1.9 / 15-除非另有说明,
对单个
运算符的操作数的评估和
个别表达式的子表达式的评估是
无序的[注:在表达式中
在程序执行期间被评价多于一次

无序和不确定地
顺序的其
子表达式的评估不需要在不同的评估中一致地执行

-end note]操作符结果的值计算
之前,操作符的操作数
的值计算是
如果a
对标量对象的副作用是
相对于对同一标量对象
的另一个
副作用或使用同一标量对象的值
的值计算,
行为未定义。



$ 3.9 / 9 - 算术类型(3.9.1),
枚举类型, ,
指向成员类型(3.9.2),
std :: nullptr_t和cv限定的
的指针这些类型(3.9.3)的版本是
,统称为标量类型。


请注意,在表达式z = z ++;中,z是标量变量,由于赋值运算符和后缀运算符++无序(它们都不在另一个之前排序)。



感谢@Prasoon提供有价值的输入, / p>

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

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

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.

解决方案

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

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.

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

In C++0x

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

In C++0X however, as I understand, there is no concept of sequence points. This expression is fine and does not invoke undefined behavior. This is because the effect of ++ on 'z' is sequenced before the side effect of assignment on 'z'.

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

$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."

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

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

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

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