error: declared here unique_ptr(const unique_ptr&) = delete;
error: declared here unique_ptr(const unique_ptr&) = delete;
这段代码编译成功。
#include<iostream>
#include<memory>
using namespace std;
class A{
public:
unique_ptr<A> myval;
A(){ cout<<"Constrcutor of A is called"<<endl; }
~A(){cout<<"Destructor of A is called"<<endl;}
unique_ptr<A> getsomething()
{
unique_ptr<A> myval;
myval.reset(new A);
return myval;
}
};
但是当我评论本地时unique_ptr<A> myval;
编译器抛出错误。
shared_test.cpp: In member function ‘std::unique_ptr<A> A::getsomething()’:
shared_test.cpp:12:10: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]’
return myval;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from shared_test.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
我无法理解这个错误的意思。这是什么?
这意味着std::unique_ptr
无法复制
让您的 return 声明移动所有权,
return std::move(myval);
或 return 对原始 std::unique_ptr
的引用/指针,尽管这不会转移所有权并且很可能不是您想要的。
要了解原始 OP 代码为何有效,请参阅 Returning unique_ptr from functions
编辑:我假设您原来的 body 是这样的:
// ..
unique_ptr<A> getsomething()
{
return myval;
}
根据您最初的提问。
您的局部变量 myval
隐藏了 class 成员 myval
。
When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.
在您的第一种情况下,返回的对象是使用 myval
创建的,就好像它是一个 rvalue
,并且由于它是一个 rvalue
,因此可以选择 [=15] =]构造函数,如果有的话。
在第二种情况下,必须进行复制,因此它直接调用 copy
构造函数,该构造函数失败,因为无法复制 unique_ptr
对象,并且您还没有使用 std::move
以确保编译器调用 move
构造函数。
这段代码编译成功。
#include<iostream>
#include<memory>
using namespace std;
class A{
public:
unique_ptr<A> myval;
A(){ cout<<"Constrcutor of A is called"<<endl; }
~A(){cout<<"Destructor of A is called"<<endl;}
unique_ptr<A> getsomething()
{
unique_ptr<A> myval;
myval.reset(new A);
return myval;
}
};
但是当我评论本地时unique_ptr<A> myval;
编译器抛出错误。
shared_test.cpp: In member function ‘std::unique_ptr<A> A::getsomething()’:
shared_test.cpp:12:10: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]’
return myval;
^
In file included from /usr/include/c++/4.8/memory:81:0,
from shared_test.cpp:2:
/usr/include/c++/4.8/bits/unique_ptr.h:273:7: error: declared here
unique_ptr(const unique_ptr&) = delete;
^
我无法理解这个错误的意思。这是什么?
这意味着std::unique_ptr
无法复制
让您的 return 声明移动所有权,
return std::move(myval);
或 return 对原始 std::unique_ptr
的引用/指针,尽管这不会转移所有权并且很可能不是您想要的。
要了解原始 OP 代码为何有效,请参阅 Returning unique_ptr from functions
编辑:我假设您原来的 body 是这样的:
// ..
unique_ptr<A> getsomething()
{
return myval;
}
根据您最初的提问。
您的局部变量 myval
隐藏了 class 成员 myval
。
When the criteria for elision of a copy operation are met and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.
在您的第一种情况下,返回的对象是使用 myval
创建的,就好像它是一个 rvalue
,并且由于它是一个 rvalue
,因此可以选择 [=15] =]构造函数,如果有的话。
在第二种情况下,必须进行复制,因此它直接调用 copy
构造函数,该构造函数失败,因为无法复制 unique_ptr
对象,并且您还没有使用 std::move
以确保编译器调用 move
构造函数。