为什么这个片段不能在VS 2013中工作?
Why does this snippet not work in VS 2013?
这段代码有什么问题吗?
#include <memory>
class Foo {
};
class Bar {
std::unique_ptr<Foo> foo_;
};
int main() {
Bar bar;
Bar bar2 = std::move(bar);
}
我得到这个错误:
1>c:usersszxdocumentsvisual studio 2013projectsconsoleapplication1consoleapplication1main.cpp(13): error C2280: 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
1> with
1> [
1> _Ty=Foo
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludememory(1486) : see declaration of 'std::unique_ptr<Foo,std::default_delete<_Ty>>::unique_ptr'
1> with
1> [
1> _Ty=Foo
1> ]
1> This diagnostic occurred in the compiler generated function 'Bar::Bar(const Bar &)'
但是GCC能够编译它而没有错误:http://ideone.com/CiDcGI
您的代码有效。VS2013拒绝它,因为编译器没有实现隐式生成move构造函数和move赋值操作符。注意,甚至不允许显式地默认它们。你唯一的选择是实现move构造函数。
class Bar {
std::unique_ptr<Foo> foo_;
public:
Bar(Bar&& b) : foo_(std::move(b.foo_)) {}
Bar() = default;
};
来自MSDN:支持c++ 11特性(现代c++)
"右值引用v3.0"增加了在特定条件下自动生成移动构造函数和移动赋值操作符的新规则。然而,由于时间和资源的限制,这在Visual Studio 2013中的Visual c++中没有实现。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 如何使Ws2_32.lib与针对Windows 8.1的VS 2013项目协同工作
- CUDA不能在Visual Studio 2013 -工具包4.2,5.5和6上工作
- Visual 2013 插件,用于记录您在项目上工作的时间。
- Cuda 7.5和Visual Studio 2013:一些例子工作,但其他返回cudaErrorDevicesUnav
- c++代码在VS2010中工作,但在2013中不工作
- std::enable_if在Visual Studio 2013中没有正常工作
- 为什么这个片段不能在VS 2013中工作?
- 智能感知和代码建议在添加c++项目后无法在Visual Studio 2013中工作
- TFS 2013构建如何使工作空间文件可编辑
- Word 2013: OLE就地编辑不工作
- Visual Studio 2013语法高亮显示停止工作
- Visual C++ 2013 程序在 IDE 中工作,但在 InstallShield LE 安装后不加载任何显示内容