Side effect of C ++ assignment

When trying to provide an increasing order of two variables, I came across an unusual anomaly in the Visual Studio 2012 C ++ compiler, which can be illustrated by the following code fragment

double x1 = 2; double x2 = 1; std::tie(x1, x2) = std::minmax(x1, x2); std::cout << "x1 = " << x1 << ", x2 = " << x2 << "\n"; 

one would expect that x1 is equal to 1, and x2 is equal to 2. But this is not so. Instead

  //output: //x1 = 1, x2 = 1 

Is there a good explanation just not to fall into such a trap again?

+6
source share
2 answers

std::minmax returns its arguments back by reference. What happens to your statement, first x1 gets the value x2 , which is 1. Then x2 gets the value x1 , which is 2, but now it is 1.

If you were to embed everything, it might look something like this:

 // using pointers because references can't be rebound double *less, *greater; if (x1 <= x2) { less = &x1; greater = &x2; } else { // in your case, this is the branch taken less = &x2; greater = &x1; } x1 = *less; // less points to x2, so now both x1 and x2 = 1 x2 = *greater; // greater points to x1, and x1 = 1, so this assignment is redundant 

I think part of your confusion comes from the thought (or hope) that appointments will occur at the same time, but they do not. When you assign a tuple or pair , sub-objects are assigned in order from left to right.

+4
source

The problem is that std::minmax() takes two links and returns a couple of links. In particular, in your case, it will return a pair, where the first element is a link to x2 , and the second element is a link to x1 .

On the left side of the assignment, on the other hand, you have:

 std::tie(x1, x2) 

Which also creates a pair of links (normal, a tuple of two links actually, but that doesn't matter), but this time the first element of the pair is a link to x1 , and the second element is a link to x2 .

Then you assign the pair returned by std::minmax() to the pair returned by std::tie , which means that x1 first gets the value x2 (which is 1 ); then x2 gets the new value x1 (again, because std::minmax() returned a pair of links, so the second element of this pair β€œsees” the side effect of assigning to x1 ), which is now 1 .

This should explain the conclusion.

+3
source

Source: https://habr.com/ru/post/948378/


All Articles