在表达式left()= right()中,为什么right()首先被排序? [英] In the expression left() = right(), why is right() sequenced first?
问题描述
在C ++中,表达式left() = right()
计算
In C++, the expression left() = right()
evaluates
-
right()
-
left()
right()
left()
按此顺序. right()
首先出现,如此处所述.
in that sequence. The right()
goes first, as has been discussed here.
我想不出right()
首先走的原因.你能?我认为这是有原因的.否则,该标准几乎不会说出它的意思,而是要考虑:right()
将返回一些结果.在机器代码级别上,CPU是否不需要在要求right()
返回结果之前就知道将结果right()
放回何处?
I cannot think of a reason for right()
to go first. Can you? I presume that there exists a reason. Otherwise, the standard would hardly say what it says, but consider: right()
will return some result. At the machine-code level, does the CPU not need to know where to put the result right()
will return before asking right()
to return it?
如果您碰巧知道标准委员会在想什么(因为您在会议室或已阅读备忘录),那就太好了:我想阅读您的答案.但是,我的实际问题较为温和.我只想知道是否存在合理的原因以及该原因可能是什么.
If you happen to know what the standard committee was thinking (because you were in the room or have read the memo), that's great: I'd like to read your answer. However, my actual question is more modest. All I want to know is whether there exists a plausible reason and what that reason might be.
推荐答案
除了执行Brian所示操作时的不直观结果之外,
In addition to the unintuitive result when doing what Brian showed:
#include <map>
int main() {
std::map<int, int> m;
m[0] = m.size(); // before C++17 m[0] could be 0 or 1 - it was implementation defined
}
如果我们使用相同的地图,但要这样做:
If we take a the same map but do:
#include <map>
int main() {
std::map<int, int> m;
m[0] = Right(); // Right() may throw
}
如果Right()
抛出:
在C ++ 17之前,您可以在m[0]
(从左到右)中获取默认的构造元素,或者根本不会创建m[0]
(从右到左).在C ++ 17中,根本不会创建m[0]
.
Before C++17 you could get a default constructed element in m[0]
(left to right) or m[0]
wouldn't be created at all (right to left). In C++17 m[0]
will not get created at all.
这篇关于在表达式left()= right()中,为什么right()首先被排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!