为什么有前缀/后缀 ++但没有前缀/后缀 +=?
这似乎是一个愚蠢的问题,但为什么在许多语言中都存在 ++
和 --
运算符的前缀和后缀版本,但没有类似的前缀/postfix 其他运算符的版本,例如 +=
或 -=
?例如,看起来如果我可以编写这段代码:
myArray[x++] = 137; // Write 137 to array index at x, then increment x
我应该能够编写类似的内容
myArray[5 =+ x] = 137; // Write 137 to array index at x, then add five to x
当然,这样的运算符不存在。这是有原因的吗?这在 C/C++/Java 中似乎是一种奇怪的不对称性。
This may seem like a silly question, but why is it that in many languages there exists a prefix and postfix version of the ++
and --
operator, but no similar prefix/postfix versions of other operators like +=
or -=
? For example, it seems like if I can write this code:
myArray[x++] = 137; // Write 137 to array index at x, then increment x
I should be able to write something like
myArray[5 =+ x] = 137; // Write 137 to array index at x, then add five to x
Of course, such an operator does not exist. Is there a reason for this? It seems like a weird asymmetry in C/C++/Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我猜想有几个原因,我认为其中权重较大的可能是:
话又说回来,虽然前/后/递增/递减运算符的想法可能受到机器操作的影响,但看起来它们并没有专门放入语言中以利用这种优势。以下是 Dennis Ritchie 对它们的评价:
http://cm.bell-labs。 com/cm/cs/who/dmr/chist.html
I'd guess there are several reasons, I think among the more heavily weighted might be:
Then again, while the idea for pre/post/increment/decrement operators might have been influenced by machine operations, it looks like they weren't put into the language specifically to take advantage of such. Here's what Dennis Ritchie has to say about them:
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html
只要
y
没有副作用:As long as
y
has no side effects:我会做一个假设。
++i
/i++
有很多用例,并且在许多用例中,特定类型的增量(前/后)会有所不同。我已经记不清有多少次看到像while (buf[i++]) {...}
这样的代码。另一方面,+=
的使用频率要低得多,因为一次将指针移动 5 个元素几乎没有意义。因此,没有足够常见的应用程序可以使
+=
的后缀版本和前缀版本之间的差异变得很重要。I'll make an assumption. There're lots of use-cases for
++i
/i++
and in many the specific type of increment (pre/post) makes difference. I can't tell how many times I've seen code likewhile (buf[i++]) {...}
. On the other hand,+=
is used much less frequently, as it rarely makes sense to shift pointer by 5 elements at once.So, there's just no common enough application where difference between postfix and prefix version of
+=
would be important.我想这是因为它太神秘了。
有些人认为甚至应该避免使用 ++/--,因为它们会引起混乱并导致大多数缓冲区溢出错误。
I guess it's because it's way too cryptic.
Some argue that even ++/-- should be avoided, because they cause confusion and are responsible for most buffer overrun bugs.
因为 -- 和 ++ 运算符映射到 CPU 中的
inc
(rement) 和dec
(rement) 指令(除了加法和减法之外),并且这些运算符是应该映射到指令,因此它们作为单独的运算符存在。Because the -- and ++ operators map to
inc
(rement) anddec
(rement) instructions (in addition to adding and subtracting) in the CPU, and these operators are supposed to map to the instructions, hence why they exist as separate operators.Java 和 C++ 具有前置和后置自增和自减运算符,因为 C 也有它们。 C 拥有它们是因为 C 主要是为 PDP-11 编写的,而 PDP-11 有
INC
和DEC
指令。过去,优化编译器还不存在,因此如果您想使用单循环增量运算符,要么为它编写汇编程序,要么您的语言需要为其显式运算符; C 是一种可移植的汇编语言,具有显式的递增和递减运算符。此外,
++i
和i++
之间的性能差异现在已经不太重要,但在 1972 年确实很重要。请记住,C 已有近 40 年的历史了。
Java and C++ have pre- and post- increment and decrement operators because C has them. C has them because C was written, mostly, for the PDP-11 and the PDP-11 had
INC
andDEC
instructions.Back in the day, optimizing compilers didn't exist so if you wanted to use a single cycle increment operator, either you wrote assembler for it or your language needed an explicit operator for it; C, being a portable assembling language, has explicit increment and decrement operators. Also, the performance difference between
++i
andi++
rarely matters now but it did matter in 1972.Keep in mind that C is almost 40 years old.
如果我不得不猜测,通常将:
x += 5;
...与:
x = x + 5;
出于显而易见的原因,这是荒谬的写:
x + 5 = x;
我不知道你还能如何仅使用
+
5 =+ x 的行为> 运算符。顺便说一句,你好!If I had to guess, it's common to equate:
x += 5;
...with:
x = x + 5;
And for obvious reasons, it would be nonsensical to write:
x + 5 = x;
I'm not sure how else you would mimic the behavior of
5 =+ x
using just the+
operator. By the way, hi htiek!