涉及逻辑 AND (&&) 的复杂表达式

发布于 2024-09-25 21:24:49 字数 262 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花期渐远 2024-10-02 21:24:49

在 C++ 03 中。

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

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

注意:请注意,运算符 && 处有序列点。但这些在这个例子中是不相关的。

美好的!。一般来说,可能是也可能不是。取决于 x 和 y 的值。在你的具体情况下,这是不好的。此代码有可能具有名为未定义行为< /a>.

如果计算 z++(如示例中所示,因为 x 和 y 为 1),则标量变量“z”在两个序列点 Seq1 和 Seq2 之间的表达式中被修改多次(见下文) 。需要注意的是,赋值运算符不会引入任何序列点。

$5/4-“除非另有说明,订单
的操作数求值
个体经营者和
个体的子表达
表达式以及顺序
发生副作用的是
unspecified.53) 在前一个之间
下一个序列指向标量
对象应有其存储值
最多修改一次
表达式的求值。
此外,先验值应为
访问仅用于确定值
存储。
此要求
每个段落均应满足
允许的排序
完整表达式的子表达式;
否则行为未定义。”

在 C++0x 中,

一旦我自己理解了 @litb 提到的讨论的细节,我就会更新它。现在,我只是将其删除

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

$1.9/15-“除非另有说明,
个别操作数的评估
运算符和子表达式
个别表达是
未排序的。 [ 注意:在表达式中
被评估多次
在程序执行期间,
无序且不确定
对其进行排序评估
不需要执行子表达式
在不同的评价中保持一致。
—尾注] 的值计算
运算符的操作数是
在值计算之前排序
算子的结果。 如果
对标量对象的副作用是
相对于另一个没有顺序
对同一标量对象的副作用
或使用值进行值计算
对于同一个标量对象,
行为未定义。

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

请注意,在表达式 'z = z++;' 中 运算符 ++ 对 'z' 的副作用是不排序的(它们都没有在另一个之前排序)。

其中 z 是标量变量,由于赋值运算符和后缀

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

心在旅行 2024-10-02 21:24:49

知道该行是否正常的一个简单方法是让编译器检查它。例如,gcc 有 -Wsequence-point 选项(由-Wall启用)用于检查是否由于缺少序列点而存在未定义的行为。

您的程序

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

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

    return 0;
}

会产生此警告:

x.c: In function 'main':
x.c:6:5: warning: operation on 'z' may be undefined

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

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

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

    return 0;
}

produces this warning:

x.c: In function 'main':
x.c:6:5: warning: operation on 'z' may be undefined
单身情人 2024-10-02 21:24:49

是的,它会编译。

但是,如果您询问逻辑错误:

1) && 运算符引入了一个序列点,因为当它确定最终结果(在本例中是0 值可以终止计算),因此如果 xy<,它甚至不会到达 ++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 a 0 value can terminate the evaluation), so it won't even reach to the ++z part if x or y 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文