What does -->> actually do?
问题是C++中的"->"运算符是什么?它询问-->做什么,并提供指向comp.lang.c++中继线程的链接。向下滚动一点,我发现:
> There is no such operator in C++.
> It's just a combination of two operators: postfix decrement"--" and
> greater">".> That's why this example works.
> Try ( x --> 20 ) and you'll get no output in this case;)
Of course there is. It is described together with"runs to" operator:
1 2 3 4 5 6 7 | #include <stdio.h> int main() { int x = 10; while( x -->> 0 ) // x runs to 0 printf("%d", x); } |
"跑向"操作员实际上做什么?
while( x -->> 0 ) // x runs to 0
这实际上是
1 | while (x-- >> 0) |
对于这个特定的用法,在右手边为0的情况下,由于后缀
当
更一般地说,如果您试图对负值(例如,
本标准相关部分:5.8/3:
The value of
E1 >> E2 isE1 right-shiftedE2 bit positions. IfE1 has an unsigned type or ifE1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient ofE1/2^E2 . IfE1 has a signed type and a negative value, the resulting value is implementation-defined.
btw/-对于Visual Studio,根据http://msdn.microsoft.com/en-us/library/336xbcz.aspx,实现定义的行为为"如果加法表达式为0,则不执行移位操作"。我在GCC手册中找不到任何关于这个的信息(应该在这里找到)。
1 | while( x -->> 0 ) // x runs to 0 |
不,"转到操作员"是
所以它"起作用",但它是表达循环的一种糟糕的方式。
--减量,但返回减量前变量的值,>>按右操作数右移,即0(a.k.a.no op),然后将结果隐式与0进行比较。