Code :
#include <iostream>
class A {
public:
A() {
}
A(const A& a) {
std::cout << "Copy constructor" << std::endl;
}
A(A&& a) {
std::cout << "Move constructor" << std::endl;
}
};
int main()
{
{//case 1
A b = A(A());
}
std::cout << std::endl;
{// case 2
A a;
A b = A(a);
}
std::cout << std::endl;
{//case 3
A a;
A b = A(std::move(a));
}
}
Output (with -O3 compilation flag) :
#case 1
#nothing printed
#case 2
Copy constructor
#case 3
Move constructor
In case 2, why is the copy constructor called even with maximum optimization level (-O3) ? I was expecting the compiler to detect that the variable 'a' is like being temporary (because used only for the construction of 'b') and to rather use the move constructor (like in case 3).
To my knowledge there is at least one case (return value optimization) where the compiler can alter the observable behavior of the program by avoiding the call to a copy constructor which has side effect.
So I was wondering if it could be possible in case 2, also for optimization purpose, to replace the call of the copy constructor by the move constructor, knowing that variable a is never used outside of the construction of b.