什么是'-->' C/C++ 中的运算符?
阅读C++/STL 的隐藏功能和黑暗角落后在 comp.lang.c++.moderated
上,我完全惊讶于以下代码片段在 Visual Studio 2008 和 G++ 4.4 中都能编译并运行。我认为这也是有效的 C,因为它也适用于 GCC。
代码如下:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
输出:
9 8 7 6 5 4 3 2 1 0
标准中在哪里定义了它,它来自哪里?
After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated
, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. I would assume this is also valid C since it works in GCC as well.
Here's the code:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
Output:
9 8 7 6 5 4 3 2 1 0
Where is this defined in the standard, and where has it come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
-->
不是运算符。它实际上是两个独立的运算符,--
和>
。条件中的代码递减
x
,同时返回x
的原始(未递减)值,然后使用以下命令将原始值与0
进行比较>
运算符。为了更好地理解,该语句可以写成如下:
-->
is not an operator. It is in fact two separate operators,--
and>
.The code in the condition decrements
x
, while returningx
's original (not decremented) value, and then compares the original value with0
using the>
operator.To better understand, the statement could be written as follows:
或者对于完全不同的东西...
x
滑动到0
。不是那么数学化,但是...每张图片都描绘了一千个单词...
Or for something completely different...
x
slides to0
.Not so mathematical, but... every picture paints a thousand words...
这是一个非常复杂的运算符,因此甚至 ISO/IEC JTC1(联合技术委员会 1) 也放置了其描述在 C++ 标准的两个不同部分中。
开玩笑吧,它们是两个不同的运算符:
--
和>
分别在 C++03 标准的 §5.2.6/2 和 §5.9 中描述。That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard.
Joking aside, they are two different operators:
--
and>
described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.在 C++ 中,
x
可以在相反方向更快地变为零:8 6 4 2
您可以用箭头控制速度!
90 80 70 60 50 40 30 20 10
x
can go to zero even faster in the opposite direction in C++:8 6 4 2
You can control speed with an arrow!
90 80 70 60 50 40 30 20 10
它相当于
x--
(后递减)相当于x = x-1
(但返回x
的原始值),所以代码转换为:It's equivalent to
x--
(post decrement) is equivalent tox = x-1
(but returning the original value ofx
), so the code transforms to:只是
空格让事情看起来很有趣,
--
递减和>
比较。It's
Just the space makes the things look funny,
--
decrements and>
compares.-->
的使用具有历史相关性。在 x86 架构上,递减曾经(并且在某些情况下仍然如此)比递增更快。使用-->
表明x
将变为0
,并且对具有数学背景的人很有吸引力。The usage of
-->
has historical relevance. Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture. Using-->
suggests thatx
is going to0
, and appeals to those with mathematical backgrounds.非常极客,但我将使用这个:
Utterly geek, but I will be using this:
我读过的一本书(我不记得是哪本书了)指出:编译器尝试使用左右规则将表达式解析为最大的标记。
在这种情况下,表达式:
解析为最大标记:
相同的规则适用于此表达式:
解析后:
One book I read (I don't remember correctly which book) stated: Compilers try to parse expressions to the biggest token by using the left right rule.
In this case, the expression:
Parses to biggest tokens:
The same rule applies to this expression:
After parse:
这与非负数的
while(x--)
完全相同。This is exactly the same as
while(x--)
for non-negative numbers.不管怎样,我们现在有了一个“gos to”操作符。
“-->”
作为方向很容易被记住,而“当x变为零”的意思是直线。此外,在某些平台上,它比
“for (x = 10; x > 0; x --)”
更高效。Anyway, we have a "goes to" operator now.
"-->"
is easy to be remembered as a direction, and "while x goes to zero" is meaning-straight.Furthermore, it is a little more efficient than
"for (x = 10; x > 0; x --)"
on some platforms.该代码首先比较 x 和 0,然后递减 x。 (在第一个答案中也说过:您要对 x 进行后递减,然后使用
>
运算符比较 x 和 0。)请参阅此代码的输出:我们现在首先比较,然后递减在输出中看到 0。
如果我们想先递减然后比较,请使用以下代码:
输出为:
This code first compares x and 0 and then decrements x. (Also said in the first answer: You're post-decrementing x and then comparing x and 0 with the
>
operator.) See the output of this code:We now first compare and then decrement by seeing 0 in the output.
If we want to first decrement and then compare, use this code:
That output is:
当我运行此代码时,我的编译器将打印出 9876543210。
正如预期的那样。
while( x-- > 0 )
实际上意味着while( x > 0)
。x--
后递减x
。是同一件事的不同写法。
很高兴原来的看起来像“当 x 变为 0”。
My compiler will print out 9876543210 when I run this code.
As expected. The
while( x-- > 0 )
actually meanswhile( x > 0)
. Thex--
post decrementsx
.is a different way of writing the same thing.
It is nice that the original looks like "while x goes to 0" though.
--
和>
之间缺少空格。x
是后递减的,即在检查条件x>0 ?
后递减。There is a space missing between
--
and>
.x
is post decremented, that is, decremented after checking the conditionx>0 ?
.--
是减量 运算符,>
是大于 运算符。这两个运算符作为一个运算符应用,如
-->
。--
is the decrement operator and>
is the greater-than operator.The two operators are applied as a single one like
-->
.它是两个运算符的组合。第一个
--
用于递减值,>
用于检查值是否大于右侧操作数。输出将是:
It's a combination of two operators. First
--
is for decrementing the value, and>
is for checking whether the value is greater than the right-hand operand.The output will be:
C 和 C++ 遵循“最大咀嚼”规则。同样的方式将
a---b
翻译为(a--) - b
,在您的情况下x-->0
会翻译到(x--)>0
。该规则本质上说的是,从左到右,表达式是通过采用将形成有效标记的最大字符来形成的。
C and C++ obey the "maximal munch" rule. The same way
a---b
is translated to(a--) - b
, in your casex-->0
translates to(x--)>0
.What the rule says essentially is that going left to right, expressions are formed by taking the maximum of characters which will form a valid token.
实际上,
x
是后递减的,并且正在检查该条件。这不是-->
,而是(x--) > 0
注意:
x
的值在检查条件后会更改,因为它是后递减的。一些类似的情况也可能发生,例如:Actually,
x
is post-decrementing and with that condition is being checked. It's not-->
, it's(x--) > 0
Note: value of
x
is changed after the condition is checked, because it post-decrementing. Some similar cases can also occur, for example:您可以使用穿甲箭头运算符来代替常规箭头运算符(-->):--x> (注意箭头尖端上的那些锋利的倒刺)。它为穿甲增加 +1,因此它比常规箭头操作员更快地完成循环 1 迭代。自己尝试一下:
Instead of regular arrow operator (-->) you can use armor-piercing arrow operator: --x> (note those sharp barbs on the arrow tip). It adds +1 to armor piercing, so it finishes the loop 1 iteration faster than regular arrow operator. Try it yourself:
对原始问题的简单回答是,以下代码执行相同的操作(尽管我并不是说您应该这样做):
x--
只是上面的简写,而 < code>> 只是一个普通的大于运算符
。The simple answer to the original question is that the following code does the same thing (though I am not saying you should do it like this):
The
x--
is just shorthand for the above, and>
is just a normal greater-thanoperator
.(x --> 0)
表示(x-- > 0)
。(x -->)
输出:9 8 7 6 5 4 3 2 1 0
(-- x > 0)
意思是(--x > 0)
输出:9 8 7 6 5 4 3 2 1
Output: 9 8 7 6 5 4 3 2 1
Output: 9 8 7 6 5 4 3 2 1 0
Output : 9 8 7 6 5 4 3 2 1 0
Output: 9 8 7 6 5 4 3 2 1 0
同样,你可以尝试很多方法来执行这个命令成功地。
(x --> 0)
means(x-- > 0)
.(x -->)
Output: 9 8 7 6 5 4 3 2 1 0
(-- x > 0)
It's mean(--x > 0)
Output: 9 8 7 6 5 4 3 2 1
Output: 9 8 7 6 5 4 3 2 1
Output: 9 8 7 6 5 4 3 2 1 0
Output: 9 8 7 6 5 4 3 2 1 0
Output: 9 8 7 6 5 4 3 2 1 0
Likewise, you can try lot of methods to execute this command successfully.
常规方式我们在 while 循环括号“
()
”中定义条件,在大括号“{}
”内定义终止条件,但是这个--
& ;>
是一种一次性定义所有内容的方式。例如:
它说,递减
a
并运行循环,直到a
大于0
其他方式应该是这样的:
两者方式,我们做同样的事情,实现同样的目标。
Conventional way we define condition in while loop parenthesis"
()
" and terminating condition inside the braces"{}
", but this--
&>
is a way one defines all at once.For example:
It says, decrement
a
and run the loop till the timea
is greater than0
Other way it should have been like:
Both ways, we do the same thing and achieve the same goals.
-->
不是运算符,它是--
(后自减)和>
(大于比较)的并置。该循环看起来更熟悉:
此循环是一个经典的习惯用法,用于枚举
10
(排除的上限)和0
包含的下限之间的值,对于迭代非常有用数组中从最后一个到第一个的元素。初始值
10
是迭代总数(例如数组的长度),加上循环内使用的第一个值。0
是循环内x
的最后一个值,因此注释 x 变为 0。请注意,循环完成后
x
的值为-1
。另请注意,如果
x
具有无符号类型(例如size_t
),则此循环将以相同方式运行,即与 (i = length-1; i >= 0; i--) 的朴素替代方案相比,这是一个强大的优势。出于这个原因,我实际上很喜欢这种令人惊讶的语法:
while (x --> 0)
。我发现这个习语引人注目且优雅,就像for (;;)
与:while (1)
(看起来与while (l) 令人困惑) )。它也适用于语法受 C 启发的其他语言:C++、Objective-C、java、javascript、C# 等等。
-->
is not an operator, it is the juxtaposition of--
(post-decrement) and>
(greater than comparison).The loop will look more familiar as:
This loop is a classic idiom to enumerate values between
10
(the excluded upper bound) and0
the included lower bound, useful to iterate over the elements of an array from the last to the first.The initial value
10
is the total number of iterations (for example the length of the array), and one plus the first value used inside the loop. The0
is the last value ofx
inside the loop, hence the comment x goes to 0.Note that the value of
x
after the loop completes is-1
.Note also that this loop will operate the same way if
x
has an unsigned type such assize_t
, which is a strong advantage over the naive alternativefor (i = length-1; i >= 0; i--)
.For this reason, I am actually a fan of this surprising syntax:
while (x --> 0)
. I find this idiom eye-catching and elegant, just likefor (;;)
vs:while (1)
(which looks confusingly similar towhile (l)
). It also works in other languages whose syntax is inspired by C: C++, Objective-C, java, javascript, C# to name a few.这里
--
是一元后减运算符。(x > 0) // 10 > 0
x-- // x = 9
x=1
,所以条件为真。根据一元运算符,该值在打印时更改为x = 0
。x = 0
,它将条件(x > 0 )
计算为 false,并且 while 循环退出。Here
--
is the unary post decrement operator.(x > 0) // 10 > 0
x-- // x = 9
x=1
, so the condition is true. As per the unary operator, the value changed tox = 0
at the time of print.x = 0
, which evaluates the condition(x > 0 )
as false and the while loop exits.这个
-->
根本不是一个运算符。我们有一个类似于->
的运算符,但不像-->
那样。这只是while(x-- >0)
的错误解释,它只是意味着 x 具有后递减运算符,并且此循环将运行直到它大于零 。编写此代码的另一种简单方法是
while(x--)
。 while循环只要遇到错误条件就会停止,这里只有一种情况,即0
。因此,当 x 值减至零时,它将停止。This
-->
is not an operator at all. We have an operator like->
, but not like-->
. It is just a wrong interpretation ofwhile(x-- >0)
which simply means x has the post decrement operator and this loop will run till it is greater than zero.Another simple way of writing this code would be
while(x--)
. The while loop will stop whenever it gets a false condition and here there is only one case, i.e.,0
. So it will stop when the x value is decremented to zero.这就是你的意思。
我们小时候就听过,
?????????????????????????????????????????????????????????????????????????????????????????????????????????
发生同样的情况,空格会造成混乱。 :D
That's what you mean.
We heard in childhood,
Where a Comma makes confusion
Same Happens in Programming now, a SPACE makes confusion. :D