使用赋值作为条件表达式?

发布于 2024-11-25 18:54:04 字数 116 浏览 1 评论 0原文

考虑一下:

if (a=5) {
   /* do something */
}

任务作为条件如何发挥作用?

它是基于左值的非零值吗?

Consider:

if (a=5) {
   /* do something */
}

How does the assignment work as a condition?

Is it based on non-zero value of l-value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

转身以后 2024-12-02 18:54:04

C++ — ISO/IEC 14882:2003(E)

[5.17/1] 有几个赋值运算符,它们全部分组
从右到左。全部都需要一个可修改的左值作为其左操作数,
赋值表达式的类型是其左操作数的类型。
赋值运算的结果是左边存储的值
赋值后的操作数
;结果是一个左值。

表达式a = 5的结果是5

[6.4/4] [..] 作为表达式的条件的值是
表达式,对于除以下以外的语句隐式转换为bool
切换。 [..]

发生到 bool 的转换。

[4.12/1] 算术、枚举、指针或指向成员的指针的右值
type 可以转换为 bool 类型的右值。零值,null
指针值,或者空成员指针值被转换为false任意
其他值转换为 true

5 转换为布尔值 true

[6.4.1/1] 如果条件 (6.4) 为 true,则第一个
子语句被执行。
[..]

true 被视为 if 语句成功。


C — ISO/IEC 9899:1999(E)

[6.5.16/3] 赋值运算符在对象中存储值
由左操作数指定。 赋值表达式具有值
赋值后的左操作数,但不是左值。 [..]


表达式a = 5的结果是5

[6.8.4.1/2] 在这两种形式中,如果
表达式与 0 比较不等于
。 [..]

5 被视为 if 语句成功。


像这样的一般

代码几乎总是一个错误;作者的意图很可能是 if (a == 5) {}。然而,有时它是故意的。你可能会看到这样的代码:

if (x = foo()) {
   cout << "I set x to the result of foo(), which is truthy";
   // ... stuff
}

C++ — ISO/IEC 14882:2003(E)

[5.17/1] There are several assignment operators, all of which group
right-to-left. All require a modifiable lvalue as their left operand,
and the type of an assignment expression is that of its left operand.
The result of the assignment operation is the value stored in the left
operand after the assignment has taken place
; the result is an lvalue.

The result of the expression a = 5 is 5.

[6.4/4] [..] The value of a condition that is an expression is the value of the
expression, implicitly converted to bool for statements other than
switch. [..]

A conversion to bool takes place.

[4.12/1] An rvalue of arithmetic, enumeration, pointer, or pointer to member
type can be converted to an rvalue of type bool. A zero value, null
pointer value, or null member pointer value is converted to false; any
other value is converted to true.

5 converts to boolean true.

[6.4.1/1] If the condition (6.4) yields true the first
substatement is executed.
[..]

true is treated as an if statement success.


C — ISO/IEC 9899:1999(E)

[6.5.16/3] An assignment operator stores a value in the object
designated by the left operand. An assignment expression has the value
of the left operand after the assignment
, but is not an lvalue. [..]

The result of the expression a = 5 is 5.

[6.8.4.1/2] In both forms, the first substatement is executed if the
expression compares unequal to 0
. [..]

5 is treated as an if statement success.


General

Code like this is almost always a mistake; the author likely intended if (a == 5) {}. However, sometimes it is deliberate. You may see code like this:

if (x = foo()) {
   cout << "I set x to the result of foo(), which is truthy";
   // ... stuff
}
半岛未凉 2024-12-02 18:54:04

每个非零值都将被视为true

所以有些人会建议你这样写,

5 == a

以避免你犯===的错误。

Every non-zero value will be considered as true.

So some people will suggest you write

5 == a

to avoid that you make mistake == by =.

风轻花落早 2024-12-02 18:54:04

if(a=x) 相当于 if(x),此外还相当于用 x 指定的 a。因此,如果表达式 x 的计算结果为非零值,则 if(x) 就变成了 if(true)。否则,它变成if(false)

在您的情况下,由于 x = 5,这意味着 f(a=5) 除了 之外还相当于 if(true) >a 分配有 5

if(a=x) is equivalent to if(x) in addition to a assigned with x. So if the expression x evaluates to a non-zero value, then if(x) simply becomes if(true). Otherwise, it becomes if(false).

In your case, since x = 5, that means f(a=5) is equivalent to if(true) in addition to a assigned with 5.

水中月 2024-12-02 18:54:04

是的,它基于分配给 a 的零/非零值。对于某些人(包括我自己)来说,在代码中使用带有副作用的表达式也被认为是不好的做法,因此提到的代码片段最好写成类似

a = 5;
...
if (a != 0) {
    ...
}

Yes, it is based on the zero/non-zero value which a is assigned. To some people (myself included) it is also considered bad practice to have expressions with side-effects in your code, so the mentioned code fragment would preferably be written as something like

a = 5;
...
if (a != 0) {
    ...
}
牵强ㄟ 2024-12-02 18:54:04

在更现代的用法中,您有时可能会看到此模式用于处理可选的:

std::optional x = ...;
if (auto v = x) {
  // Block only executes if x contained a value, accessible as *v
}

In more modern usage, you may sometimes see this pattern used to handle optionals:

std::optional x = ...;
if (auto v = x) {
  // Block only executes if x contained a value, accessible as *v
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文