Why does r-value reference to object generator call require copy constructor?

c++, c++11, rvalue-reference, visual-c++, visual-studio-2010

Solution

It's a bug in the optimizer. The compiler attempts to ellide the move, but is only programmed to ellide a copy constructor- which requires a copy constructor to exist to be ellided in the first place.

I don't recall what (if any) the fix for this error is, but it might have been fixed in SP1.

Problem

I'm getting trouble with the following code with Visual Studio 2010 C++. makeA() is just an object generator idiom in C++ (like std::make_pair) ``` #include <stdio.h> struct A{ // 7th line A() {} A(A &&) {printf("move\n");} ~A() {printf("~A();\n");} private: A(const A &) {printf("copy\n");} // 12th line }; A makeA() { return A(); } int main() { A &&rrefA(makeA()); // 22nd line return 0; } ``` Error message ``` 2>d:\test.cpp(22): error C2248: 'A::A' : cannot access private member declared in class 'A' 2> d:\test.cpp(12) : see declaration of 'A::A' 2> d:\test.cpp(7) : see declaration of 'A' 2> ``` I expect makeA() to call both A() constructor and A(A &&) constructor, and 22nd line to call makeA() and nothing else. (If without RVO) The compiler should not require A(const A &) constructor to be accessible, am I right? Can you tell me what's wrong with the code? With recent version of g++, 'g++ -std=c++0x' and 'g++ -std=c++0x -fno-elide-constructors' compiles the code without any error.

Original source

Related problems