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.
It is necessary to pass object as reference and not by value because if you pass it by value its copy is constructed using the copy constructor. This means the copy constructor would call itself to make copy. This process will go on until the compiler runs out of memory.
When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference. One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally modified.
The point of "pass by reference" is to not make a copy as soon as Employee constructor is called, but only when you choose to initialize one of Employee's member with the Date passed.
A move constructor enables the resources owned by an rvalue object to be moved into an lvalue without copying.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With