English 中文(简体)
利用转让作为条件表述?
原标题:Using assignment as a condition expression?

考虑:

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

转让如何作为条件发挥作用?

它是否基于非零价值?

问题回答

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.

<代码>a = 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. [..]

改成<代码>bool 发生。

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

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

<代码>true作为if成功发言。


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. [..]

<代码>a = 5 。

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

<代码>5作为<>条码>成功处理。


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
}

Every non-zero value will be considered as true.

因此,有些人会建议你写此信。

5 == a

避免你犯错=,=

。 因此,如果<代码>x对非零价值进行评价,那么if(x)就简单地成为if(true)。 否则,它就成为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.

是的,它依据的是所分配的零/非零价值。 对一些人(包括我在内)来说,在你们的法典中用副作用表述也被认为是不好的做法,因此,上述法典碎片最好像文字一样。

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

在更现代的使用中,你有时会看到这种处理<条形码>的样式:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签