简体   繁体   English

为什么递增和递减是一元运算

[英]Why increment and decrement are unary operations

Looks like it's a very strange question, because i've read a lot of documentation, where increment & decrement are unary operations without any explanation.看起来这是一个非常奇怪的问题,因为我已经阅读了很多文档,其中增量和减量是一元运算,没有任何解释。

I could be wrong, but ++i is similar with i+=1 (if there aren't any overriding):我可能是错的,但++ii+=1类似(如果没有任何覆盖):

int i = 1;
Console.WriteLine(++i); // 2

int j = 1;
Console.WriteLine(j+=1); // 2

In this case, preincrement is simple syntatic sugar to hide binary operator plus and 1 as second argument.在这种情况下,预增量是简单的语法糖,用于隐藏二元运算符 plus 和1作为第二个参数。

Isn't it?不是吗?

Why does increment and decrement are independent unary operations,- isn't it just binary plus operator with predefined second argument with value 1 ?为什么递增和递减是独立的一元运算,它不只是带有预定义第二个参数值为1二元加运算符?

Your question boils down to why ++ and -- exist in the first place, when normal + and - could do the job.您的问题归结为为什么++--首先存在,而正常的+-可以完成这项工作。

With today's compiler optimisation capabilities, it's really all for historical reasons.以今天的编译器优化能力,真的都是历史原因。 ++ and -- date back to the early (but not earliest ) days of C. The Development of the C Language by late Dennis Ritchie , author of the C language, gives some interesting historical insights: ++--可以追溯到 C 的早期(但不是最早)时代。 C 语言的作者Dennis Ritchie已故的《C 语言的发展》给出了一些有趣的历史见解:

Thompson went a step further by inventing the ++ and -- operators, which increment or decrement;汤普森更进一步,发明了++--运算符,它们可以递增或递减。

[...] [...]

They were not in the earliest versions of B , but appeared along the way.它们不在B的最早版本中,而是沿途出现。

[...] [...]

a stronger motivation for the innovation was probably his observation that the translation of ++x was smaller than that of x=x+1 .更强烈的创新动机可能是他观察到++x的翻译小于x=x+1

So the definite reason seems to be lost in the mists of history, but this article by Ritchie strongly suggests that increment and decrement operators owe their existence to performance issues with early compilers.因此,确定的原因似乎已经在历史的迷雾中迷失了,但是 Ritchie 的这篇文章强烈建议自增和自减运算符的存在是由于早期编译器的性能问题。

When C++ was invented, compatibility with C was one of the major design goals by its inventor Bjarne Stroustrup , so it's needless to mention that all C operators also exist in C++.当 C++ 被发明时,与 C 的兼容性是其发明者Bjarne Stroustrup的主要设计目标之一,因此不用说所有 C 运算符也存在于 C++ 中。 As Stroustrup himself says in his FAQ :正如 Stroustrup 本人在他的常见问题解答中所说:

I wanted C++ to be compatible with a complete language with sufficient performance and flexibility for even the most demanding systems programming.我希望 C++ 与完整的语言兼容,即使是最苛刻的系统编程也具有足够的性能和灵活性。

As for C#, one its inventors Eric Lippert once stated here on the Stack Exchange network that the only reason for them being supported in C# is consistency with older languages:至于 C#,其发明者Eric Lippert 曾在 Stack Exchange 网络上表示,C# 支持它们的唯一原因是与旧语言的一致性:

[...] these operators are horrid features. [...]这些运营商是可怕的功能。 They're very confusing;它们非常令人困惑; after over 25 years I still get pre- and post- semantics mixed up. 25 年后,我仍然混淆了前后语义。 They encourage bad habits like combining evaluation of results with production of side effects.他们鼓励坏习惯,例如将结果评估与产生副作用相结合。 Had these features not been in C/C++/Java/JavaScript/etc, they would not have been invented for C#.如果这些特性不在 C/C++/Java/JavaScript/etc 中,它们就不会为 C# 发明。


PS: C++ is special because, as you have mentioned (even with the incorrect word "overriding"), you can overload all of those operators, which has lead to ++ and -- taking on slightly different semantics in the minds of many programmers. PS:C++ 很特别,因为正如您所提到的(即使使用不正确的“覆盖”一词),您可以重载所有这些运算符,这导致++--在许多程序员的脑海中呈现出略有不同的语义. They sometimes read as "go ahead" ("go back") or "make one step forward" ("make one step backward"), typically with iterators.它们有时读作“前进”(“后退”)或“前进一步”(“后退一步”),通常使用迭代器。 If you look at the ForwardIterator concept in C++, you will see that only the unary ++ is required by it.如果您查看 C++ 中的ForwardIterator概念,您会发现它只需要一元++

The answer is very simple.答案很简单。

Unary Operation means the operator will do the operations on only on operand.一元运算意味着运算符将只对操作数进行运算。

Next i++ and i+=1 both are different actions.接下来 i++ 和 i+=1 都是不同的动作。

-> when i++ will executes you the compiler will goes the variable location and it will increment the value. -> 当 i++ 将执行您时,编译器将转到变量位置并增加该值。

-> i+=1 executes the i and 1 will load into the register/temparory variable and the addition operation will done and the new value will copied into the i address loaction. -> i+=1 执行 i 并且 1 将加载到寄存器/临时变量中,加法操作将完成,新值将复制到 i 地址位置。

So compare to i+=1 will be cost compare to i++.因此,与 i+=1 相比将是与 i++ 相比的成本。

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

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