"error: use of deleted function" when calling std::move on unique_ptr in move constructor
"error: use of deleted function" when calling std::move on unique_ptr in move constructor
#include <memory>
class A
{
public:
A()
{
}
A( const A&& rhs )
{
a = std::move( rhs.a );
}
private:
std::unique_ptr<int> a;
};
此代码无法使用 g++ 4.8.4 进行编译并抛出以下错误:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>
::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_de
lete<int>]’
a = std::move( rhs.a );
^
我知道 unique_ptr 的复制构造函数和复制赋值构造函数已被删除,无法调用,但是我认为在这里使用 std::move
我会调用移动赋值构造函数。 The official documentation 甚至显示正在完成这种类型的作业。
我没有看到我的代码有什么问题?
A( const A&& rhs )
// ^^^^^
删除 const
-- 从一个对象移动是破坏性的,所以你不能从一个 const
对象移动是公平的。
#include <memory>
class A
{
public:
A()
{
}
A( const A&& rhs )
{
a = std::move( rhs.a );
}
private:
std::unique_ptr<int> a;
};
此代码无法使用 g++ 4.8.4 进行编译并抛出以下错误:
error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>
::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_de
lete<int>]’
a = std::move( rhs.a );
^
我知道 unique_ptr 的复制构造函数和复制赋值构造函数已被删除,无法调用,但是我认为在这里使用 std::move
我会调用移动赋值构造函数。 The official documentation 甚至显示正在完成这种类型的作业。
我没有看到我的代码有什么问题?
A( const A&& rhs )
// ^^^^^
删除 const
-- 从一个对象移动是破坏性的,所以你不能从一个 const
对象移动是公平的。