Inyoung Kim 김인영
Inyoung Kim 김인영

Reputation: 1586

In what order does this happen

Lets say I have an instance called box1 , box2 and run the code below.

if(box1->getSize() > box2->copyBox(box1)->getSize())

getSize() returns size of box, copyBox(box) copies the data of box1 to box2 not the address.

In what order does the code happen? I thought

  1. box1->getSize() : The size of box1 is returned
  2. box2->copyBox(box1) : box2 now shares the same address as box1 as in they're the same instance
  3. box2->getSize() : The size of box2 is returned
  4. > operator : size of box1 and box2 is compared

I can't find what the orders are with VS2017 debugger. Can anyone tell me a way to find the order with the debugger or at least what the orders are in this example? Thanks.

Upvotes: 1

Views: 80

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

No.

copyBox cannot change the address of box2.

getSize(10) is not in the above expression you are breaking down.

There are no guarantees about the order of evaluation of the lhs and the rhs of >.

Given exprA > exprB, the compiler could evaluate exprB first or exprA. Prior to C++17 it could even evakuate part of exprB, pause, do par of exprA, the continue in exprB; this may have changed in C++17 (it did in some similar contexts, and I am not certain here).

It must evaluate both exprA and exprB before >.

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.

Upvotes: 4

Related Questions