gpt4 book ai didi

c++ - 错误 : call to implicitly-deleted copy constructor of unique_ptr with auto

转载 作者:行者123 更新时间:2023-11-30 02:21:33 26 4
gpt4 key购买 nike

我有两个模板声明如下:

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 语句条件:

  1. 可以将 unique_ptr 分配给引用。

  2. auto 关键字应该推导出左值表达式的类型 rPN成为右值表达式的类型 pN->getParent() ,它返回一个类型 unique_ptr<node<T>>&.

所以我不确定这个错误的来源。

谁能指出相同的点?

最佳答案

  1. 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>>&
  1. It is possible to assign a unique_ptr to a reference.

当然可以。问题是 rPN不是引用。

关于c++ - 错误 : call to implicitly-deleted copy constructor of unique_ptr with auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48159834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com