简体   繁体   English

为什么会编译? [C ++]

[英]Why does this compile? [C++]

I tried to look for an answer online but couldn't quite find it. 我试图在网上寻找答案,但找不到。

Today I saw these lines of code: 今天,我看到了以下代码行:

    int main(){
    int n = 7;
    while(n /= 10);
    }

It doesn't make much sense, but the question was only 'will it compile?'. 这没有多大意义,但问题仅仅是“会编译吗?”。 To which I answered no, but I was wrong. 我对此没有回答,但是我错了。

My question is, why? 我的问题是,为什么? Why does 为什么

    n /= 10

behave like a bool (or an int) here? 在这里表现像布尔(或整数)?

An assignment (including a compound assignment like /= ) is an expression, which yields the value that was assigned 1 . 赋值(包括类似/=的复合赋值)是一个表达式,其产生被赋值为1的值。

So, you can do something like: x = y = z = 0; 因此,您可以执行以下操作: x = y = z = 0; , which assigns 0 to z , takes the result of that (also 0) and assigns it to y , and takes the result of that (still 0) and assigns it to x . ,将0分配给z ,将其结果(也为0)分配给y ,并将其结果(仍为0)分配给x

From there, it's making use of the implicit conversion from int to bool , in which 0 converts to false , and any non-zero value converts to true . 从那里开始,它利用从intbool的隐式转换,其中0转换为false ,任何非零值都转换为true


1. Note: that's what happens for built-in types. 1.注意:内置类型就是这种情况。 By convention, when/if you overload operator= for a class, you have it return *this; 按照惯例,当/如果您对一个类重载operator= ,可以让它return *this; , so it works the same way, as a user would/will expect--but that part's not mandatory--you can overload your operator= to return a different value or an entirely different type--but this is almost always a bad idea and should usually be avoided. ,因此它的工作方式与用户期望/将要达到的预期方式相同-但那不是强制性的-您可以使 operator=重载以返回不同的值或完全不同的类型-但这几乎总是一个坏主意并且通常应避免使用。

What you have here for your while loop is as follows: while循环在这里的内容如下:

while ( expression );

If the expression is true or non 0 the loop will continue; 如果表达式为true non 0则循环将继续;否则,循环将继续。 otherwise if it evaluates to false or 0 it will terminate. 否则,如果结果为false0 ,它将终止。 So looking back at your original: 因此,回顾一下您的原始照片:

int n = 7;
while ( n /= 10 );

This then becomes: 然后变成:

while ( n = 7 / 10 ); 

Here the full expression is n = 7 / 10 ; 这里的完整表达式是n = 7 / 10 ; This should result in 0 due to truncation of integer arithmetic. 由于整数算术的截断,这将导致0 The value by implicit conversion from int to bool becomes false . 通过从intbool的隐式转换,该值将变为false As the yielded result is 0 . 由于得出的结果是0

There is nothing here preventing this from compiling. 这里没有阻止它编译的内容。 As this is no different than having: 因为这与拥有相同:

while ( false );

However with assignment and arithmetic operations; 但是具有赋值和算术运算; this may not always be the case, but in your case it is. 可能并非总是如此,但您的情况确实如此。 Consider this next example: This will still compile but the loop will not terminate: 考虑下一个示例:它仍然可以编译,但是循环不会终止:

int n = 5;
while( n + n );

This will then become: 然后将变为:

while( 5 + 5 );
...
while( 10 );
...
while( true );

Which will still compile but the loop will continue infinitely. 仍然可以编译,但是循环将无限继续。

Just like += and -= work, *= and /= work. 就像+=-=有效, *=/=有效。

In fact, there are also &= and |= . 实际上,也有&=|=

These all evaluate to the new value that has been assigned. 这些都将评估为已分配的新值。

And, as you know, you don't have to put a boolean in a while / for / if condition; 而且,如您所知,您不必while / for / if条件中放置一小段布尔值; you only need to put something there that can be converted to a boolean. 您只需要在此处放置可以转换为布尔值的内容即可。

For example, if (42) , or for (char* ptr = begin; ptr; ++ptr) , or while (n /= 10) . 例如, if (42)for (char* ptr = begin; ptr; ++ptr)while (n /= 10)

C++ will convert the n /= 10 to bool. C ++会将n / = 10转换为bool。 Integers = 0 converted to bool evaluates to false. 将Integers = 0转换为bool会得出false。 Integers != 0 converted to bool evaluates to true. 整数!= 0转换为bool的结果为true。 That while will be evaluated as while(false) . 该while将被评估为while(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM