kcm*_*700 8 c++ visual-studio-2010 rvalue-reference visual-c++ c++11
我在使用Visual Studio 2010 C++时遇到以下代码问题.
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;
}
Run Code Online (Sandbox Code Playgroud)
错误信息
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>
Run Code Online (Sandbox Code Playgroud)
我希望makeA()能同时调用A()构造函数和A(A &&)构造函数,并且第22行调用makeA()而不是其他任何东西.(如果没有RVO)编译器不应该要求A(const A&)构造函数可访问,对吗?
你能告诉我代码有什么问题吗?
使用最新版本的g ++,'g ++ -std = c ++ 0x'和'g ++ -std = c ++ 0x -fno-elide-constructors'编译代码时没有任何错误.
这是优化程序中的错误。编译器尝试删除该移动,但仅被编程为删除副本构造函数-首先需要存在一个副本构造函数以省略它。
我不记得此错误的解决方法(如果有),但是它可能已在SP1中修复。
归档时间: |
|
查看次数: |
392 次 |
最近记录: |