[英]In what order does this happen
Lets say I have an instance called box1
, box2
and run the code below. 假设我有一个名为box1
, box2
的实例,然后运行下面的代码。
if(box1->getSize() > box2->copyBox(box1)->getSize())
getSize()
returns size of box, copyBox(box)
copies the data of box1 to box2 not the address. getSize()
返回box的大小, copyBox(box)
将box1的数据复制到box2而不是地址。
In what order does the code happen? 代码以什么顺序发生? I thought 我想
box1->getSize()
: The size of box1
is returned box1->getSize()
:返回box1
的大小 box2->copyBox(box1)
: box2
now shares the same address as box1
as in they're the same instance box2->copyBox(box1)
: box2
现在与box1
共享相同的地址,因为它们是同一实例 box2->getSize()
: The size of box2
is returned box2->getSize()
:返回box2
的大小 >
operator : size of box1
and box2
is compared >
运算符:比较box1
和box2
大小 I can't find what the orders are with VS2017 debugger. 我找不到VS2017调试器的订单。 Can anyone tell me a way to find the order with the debugger or at least what the orders are in this example? 谁能告诉我一种使用调试器查找订单的方法,或者至少在此示例中是什么? Thanks. 谢谢。
No. 没有。
copyBox
cannot change the address of box2
. copyBox
不能更改box2
的地址。
getSize(10)
is not in the above expression you are breaking down. getSize(10)
不是您要分解的上述表达式。
There are no guarantees about the order of evaluation of the lhs and the rhs of >
. 无法保证>
的lhs和rhs的评估顺序。
Given exprA > exprB
, the compiler could evaluate exprB
first or exprA
. 给定exprA > exprB
,编译器可以首先评估exprB
或exprA
。 Prior to C++17 it could even evakuate part of exprB
, pause, do par of exprA
, the continue in exprB
; 此前C ++ 17它可能甚至evakuate部分exprB
,暂停,做的票面exprA
,在继续exprB
; this may have changed in C++17 (it did in some similar contexts, and I am not certain here). 这在C ++ 17中可能已经更改(它在某些类似的上下文中确实发生过,并且我在这里不确定)。
It must evaluate both exprA
and exprB
before >
. 在>
之前,它必须同时评估exprA
和exprB
。
This unspecified execution order exists to permit different compilers solving the problem differently. 存在这种未指定的执行顺序,以允许不同的编译器以不同的方式解决问题。 It gives freedom to optimize, both in a given expression, and in how the compiler handles low level details like calling conventions. 它使您可以自由地进行优化,既可以在给定的表达式中进行选择,也可以在编译器如何处理底层细节(如调用约定)方面进行优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.