涉及逻辑 AND (&&) 的复杂表达式
void main(void)
{
int x,y,z;
x=y=z=1;
z = x && y && ++z;//is this fine?
}
我最近开始阅读有关序列点的内容,但我无法确定上面的代码示例是否正确。我知道 &&
运算符引入了一个序列点,因此我不太确定表达式 z = x && 的行为y&& ++z。有人请告诉我正确答案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 03 中。
注意:请注意,运算符 && 处有序列点。但这些在这个例子中是不相关的。
美好的!。一般来说,可能是也可能不是。取决于 x 和 y 的值。在你的具体情况下,这是不好的。此代码有可能具有名为未定义行为< /a>.
如果计算 z++(如示例中所示,因为 x 和 y 为 1),则标量变量“z”在两个序列点 Seq1 和 Seq2 之间的表达式中被修改多次(见下文) 。需要注意的是,赋值运算符不会引入任何序列点。
在 C++0x 中,
一旦我自己理解了 @litb 提到的讨论的细节,我就会更新它。现在,我只是将其删除
但是在 C++0X 中,如据我所知,没有序列点的概念。这个表达式很好,不会调用未定义的行为,这是因为 ++ 对“z”的影响在“z”上赋值的副作用之前进行排序。请注意,在表达式 'z = z++;' 中 运算符 ++ 对 'z' 的副作用是不排序的(它们都没有在另一个之前排序)。
其中 z 是标量变量,由于赋值运算符和后缀
In C++ 03.
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.
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'.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
知道该行是否正常的一个简单方法是让编译器检查它。例如,gcc 有
-Wsequence-point
选项(由-Wall
启用)用于检查是否由于缺少序列点而存在未定义的行为。您的程序
会产生此警告:
A simple way to know if that line is fine or not is let the compiler check that. For example, gcc has the
-Wsequence-point
option (enabled by-Wall
) for checking if there's undefined behavior because of lack of sequence points.Your program
produces this warning:
是的,它会编译。
但是,如果您询问逻辑错误:
1)
&&
运算符引入了一个序列点,因为当它确定最终结果(在本例中是0
值可以终止计算),因此如果x
或y<,它甚至不会到达
++z
部分/code> 为零。2)因为
&&
运算符是逻辑运算符,所以结果将始终为 0 或 1,我怀疑这是否是您想要的。Yes, it will compile.
But if you are asking about logical bugs:
1) the
&&
operator introduces a sequence point because it could terminate the evaluation of the expression when it knows for sure the final result (in this case a0
value can terminate the evaluation), so it won't even reach to the++z
part ifx
ory
is zero.2) because the
&&
operator is a logical one, the result will always be 0 or 1, and I doubt that this is what you wanted.