error: call to implicitly-deleted copy constructor of unique_ptr with auto
error: call to implicitly-deleted copy constructor of unique_ptr with auto
我有两个模板声明如下:
template < typename T >
class node {
public:
...
const unique_ptr < node < T >> & getParent();
...
private:
...
unique_ptr < node < T >> & P; //for parent node
...
};
template < typename T1, typename T2 >
class tree {
public:
tree(int, T2 & );
...
void travPreord();
...
private:
...
};
在 public 方法的定义内
template<typename T1, typename T2> tree<T1,T2>::travPreord()
我有以下代码:
template < typename T1, typename T2 >
void tree < T1, T2 > ::travPreord() {
//lambda definition
function < void(unique_ptr < node < T1 >> & ) > prntNode = [ & ]
(unique_ptr < node < T1 >> & pN) {
...
if(auto rPN = pN - > getParent()) {
...
}
};
}
对于上述 if 语句条件中的赋值,我从编译器 (g++ 4.2.1) 得到以下错误:
error: call to implicitly-deleted copy constructor of
'std::__1::unique_ptr >,
std::__1::default_delete > > >'
if(auto rPN = pN->getParent()){
错误中突出显示的模板实例化的参数由 main() 提供:
int main() {
vector < string > v;
...
tree < string, vector < string >> T(v.size(), v);
T.travPreord();
...
return 0;
}
我根据以下假设编写了问题中的 if 语句条件:
可以将 unique_ptr 分配给引用。
auto 关键字应该将左值表达式 rPN
的类型推断为右值表达式 pN->getParent()
的类型,其中 returns 类型 unique_ptr<node<T>>&.
所以我不确定这个错误的来源。
有人能指出相同的点吗?
- The auto keyword should deduce the type of the lvalue expression
rPN
to be the type of the rvalue expression pN->getParent()
, which returns a type unique_ptr<node<T>>&
.
不,类型将是 unique_ptr<node<T>>
。
Auto type deduction 应用与模板参数推导相同的规则;表达式的引用部分(即 pN->getParent()
)被忽略,之后 const
部分也被忽略。所以结果类型是unique_ptr<node<T>>
.
您需要明确指出它应该是一个引用,例如
auto& rPN = pN - > getParent() // rPN is of type const unique_ptr<node<T>>&
- It is possible to assign a unique_ptr to a reference.
当然可以。问题是 rPN
不是引用。
我有两个模板声明如下:
template < typename T >
class node {
public:
...
const unique_ptr < node < T >> & getParent();
...
private:
...
unique_ptr < node < T >> & P; //for parent node
...
};
template < typename T1, typename T2 >
class tree {
public:
tree(int, T2 & );
...
void travPreord();
...
private:
...
};
在 public 方法的定义内
template<typename T1, typename T2> tree<T1,T2>::travPreord()
我有以下代码:
template < typename T1, typename T2 >
void tree < T1, T2 > ::travPreord() {
//lambda definition
function < void(unique_ptr < node < T1 >> & ) > prntNode = [ & ]
(unique_ptr < node < T1 >> & pN) {
...
if(auto rPN = pN - > getParent()) {
...
}
};
}
对于上述 if 语句条件中的赋值,我从编译器 (g++ 4.2.1) 得到以下错误:
error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr >, std::__1::default_delete > > >' if(auto rPN = pN->getParent()){
错误中突出显示的模板实例化的参数由 main() 提供:
int main() {
vector < string > v;
...
tree < string, vector < string >> T(v.size(), v);
T.travPreord();
...
return 0;
}
我根据以下假设编写了问题中的 if 语句条件:
可以将 unique_ptr 分配给引用。
auto 关键字应该将左值表达式
rPN
的类型推断为右值表达式pN->getParent()
的类型,其中 returns 类型unique_ptr<node<T>>&.
所以我不确定这个错误的来源。
有人能指出相同的点吗?
- The auto keyword should deduce the type of the lvalue expression
rPN
to be the type of the rvalue expressionpN->getParent()
, which returns a typeunique_ptr<node<T>>&
.
不,类型将是 unique_ptr<node<T>>
。
Auto type deduction 应用与模板参数推导相同的规则;表达式的引用部分(即 pN->getParent()
)被忽略,之后 const
部分也被忽略。所以结果类型是unique_ptr<node<T>>
.
您需要明确指出它应该是一个引用,例如
auto& rPN = pN - > getParent() // rPN is of type const unique_ptr<node<T>>&
- It is possible to assign a unique_ptr to a reference.
当然可以。问题是 rPN
不是引用。