This is a follow-up question of my previous question.
Consider the following toy code:
#include <iostream>
using namespace std;
class X
{
public:
X() { }
X(X&& x)
{
cout << "move ctor\n";
}
/*X(X& x)
{
cout << "copy ctor\n";
}*/
};
X f()
{
static X x;
X&& y = std::move(x);
X& z = x;
return y;
}
int main()
{
f();
}
From my understanding on my previous question (i.e. class.copy.elision#3), I think it would cause an error (use of deleted function 'constexpr X::X(const X&)'
) in the above code to return y
in f()
.
The thing is, I run the code in Visual Studio on my PC and it compiles and prints move ctor
. So I test the code online using other compilers, and the results are that msvc and clang compile successfully while gcc gives the error that I'm expecting.
May I humbly ask if this is a bug of msvc and clang, and the program ought not to compile?