考虑:
if (a=5) {
/* do something */
}
转让如何作为条件发挥作用?
它是否基于非零价值?
考虑:
if (a=5) {
/* do something */
}
转让如何作为条件发挥作用?
它是否基于非零价值?
[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 tobool
for statements other thanswitch
. [..]
改成<代码>bool 发生。
[4.12/1]
An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of typebool
. A zero value, null pointer value, or null member pointer value is converted tofalse
; any other value is converted totrue
.
[6.4.1/1]
If the condition (6.4) yields true the first substatement is executed. [..]
<代码>true作为if
成功发言。
[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代码>作为<>条码>成功处理。
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
避免你犯错=,=。
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
}
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?