Consider this code:
#include <iostream>
struct Test
{
int x;
int y;
};
Test func(const Test& in)
{
Test out;
out.x=in.y;
out.y=in.x;
return out;
}
int main()
{
Test test{1,2};
std::cout << "x: " << test.x << ", y: " << test.y << "\n";
test=func(test);
std::cout << "x: " << test.x << ", y: " << test.y << "\n";
}
One would expect an output like this:
x: 1, y: 2
x: 2, y: 1
and this is indeed what I get. But due to copy elision, could out
be in the same place in memory as in
and result in the last line of output being x: 2, y: 2
?
I've tried compiling with gcc and clang with both -O0
and -O3
, and the results still look as intended.