C++11使用带有自定义deleter的unique_ptr
C++11 using unique_ptr with custom deleter
我正在尝试通过执行一个简单的链表程序来学习C++11 unique_ptr的用法。就我而言,我不明白为什么在使用自定义删除程序时会出现编译错误。
#include <cstdio>
#include <limits>
#include <memory>
#include <cstdlib>
#include <iostream>
using namespace std;
struct node {
int value;
struct node* next;
};
typedef struct node Node;
std::unique_ptr<Node> createList()
{
std::unique_ptr<Node> head(new Node);
Node* temp=head.get();
temp->value=0;
for(int i=1;i<8;i++) {
if(temp->next==nullptr) {
temp->next=new Node();
temp=temp->next;
temp->value=i;
temp->next=nullptr;
}
//temp=temp->next;
}
return head;
}
int main()
{
auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };
std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);
}
这是的编译错误
sh-4.3$ g++ -std=c++11 -o main *.cpp
main.cpp: In function 'int main()':
main.cpp:38:82: error: no matching function for call to 'std::unique_ptr<node, main()::<lambda(Node*)> >::unique_ptr(std::remove_reference<std::unique_
ptr<node> >::type, main()::<lambda(Node*)>&)'
std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);
^
In file included from /usr/include/c++/5.3.1/memory:81:0,
from main.cpp:3:
/usr/include/c++/5.3.1/bits/unique_ptr.h:228:2: note: candidate: template<class _Up, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::auto_ptr<_Up>&&)
unique_ptr(auto_ptr<_Up>&& __u) noexcept;
^
/usr/include/c++/5.3.1/bits/unique_ptr.h:228:2: note: template argument deduction/substitution failed:
main.cpp:38:82: note: 'std::remove_reference<std::unique_ptr<node> >::type {aka std::unique_ptr<node>}' is not derived from 'std::auto_ptr<_Up>'
std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);
^
In file included from /usr/include/c++/5.3.1/memory:81:0,
from main.cpp:3:
/usr/include/c++/5.3.1/bits/unique_ptr.h:220:2: note: candidate: template<class _Up, class _Ep, class> std::unique_ptr<_Tp, _Dp>::unique_ptr(std::uniqu
e_ptr<_Up, _Ep>&&)
unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
^
/usr/include/c++/5.3.1/bits/unique_ptr.h:220:2: note: template argument deduction/substitution failed:
main.cpp:38:82: note: candidate expects 1 argument, 2 provided
std::unique_ptr< Node, decltype(del1) > head(std::move(createList()),del1);
有什么想法吗?
您应该从createList
:返回正确的类型
#include <cstdio>
#include <limits>
#include <memory>
#include <cstdlib>
#include <iostream>
using namespace std;
struct node {
int value;
struct node* next;
};
typedef struct node Node;
auto createList()
{
auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };
std::unique_ptr< Node, decltype(del1) > head(new Node,del1);
Node* temp=head.get();
temp->value=0;
for(int i=1;i<8;i++) {
if(temp->next==nullptr) {
temp->next=new Node();
temp=temp->next;
temp->value=i;
temp->next=nullptr;
}
//temp=temp->next;
}
return head;
}
int main()
{
auto node = createList();
}
否则,在问题中显示的代码中,您应该获得内部数据的所有权并移动它们,作为不同类型的指针:
int main()
{
auto del1 = [](Node* p) { while(p) {std::cout << "Deleting value is : " << p->value;struct node* n=p->next;delete p; p=n;} return; };
std::unique_ptr< Node, decltype(del1) > head(createList().release(),del1);
}
注意对.release()
的调用
请参阅此处了解更多详细信息。
createList()
返回一个std::unique_ptr<Node>
。您尝试使用的构造函数将Node*
作为第一个参数:
unique_ptr( pointer p, /* see below */ d1 ); (3)
unique_ptr( pointer p, /* see below */ d2 ); (4)
因此出现了错误。
如果您想将自定义deleter保留在main()
的本地,您只需要将指针从createList
:中拉出即可
std::unique_ptr<Node, decltype(del1)> head(
createList().release(), // NB release(), not get()!
del1);
或者改变CCD_ 8本身以返回CCD_。
是一种替代设计
struct Node {
Node(int value) : value(value) {}
int value;
std::unique_ptr<Node> next;
};
std::unique_ptr<Node> createList()
{
std::unique_ptr<Node> head = std::make_unique<Node>(0);
Node* node = head.get();
for(int i = 1; i < 8; i++) {
node->next = std::make_unique<Node>(i);
node = node->next.get();
}
return head;
}
unique_ptr
的双参数构造函数接受一个原始指针和一个deleter,而不是智能指针和deleter。
为了确保您的示例安全,createList
可能应该返回一个unique_ptr
,它将首先删除所有节点。
相关文章:
- 为什么 std::unique 不调用 std::sort?
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 生成"unique"矩阵
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 我对 std::unique(算法)C++有问题
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- std::shared_ptr::unique(),复制和线程安全
- 如何在C++03中用自定义谓词调用std::unique
- C++中的指针否定 (!ptr == NULL)
- C++14 unique_ptr并使用已删除的函数'std::unique-ptr' unique_ptr错误