Visual Studio 2010C++で次のコードに問題が発生しています。
makeA()は、C ++のオブジェクトジェネレータイディオムです(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;
}
エラーメッセージ
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>
makeA()はA()コンストラクターとA(A &&)コンストラクターの両方を呼び出し、22行目はmakeA()だけを呼び出すことを期待しています。(RVOがない場合)コンパイラーは、A(const A&)コンストラクターがアクセス可能であることを要求するべきではありません、私は正しいですか?
コードの何が問題になっているのか教えていただけますか?
最近のバージョンのg++では、「g ++ -std = c++0x」および「g++-std = c ++ 0x -fno-elide-constructors」は、エラーなしでコードをコンパイルします。