C++11 使用带有自定义删除器的 unique_ptr

标签 c++ c++11 std smart-pointers

我正在尝试通过一个简单的链表程序来学习 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() 的调用。
参见 here了解更多详情。

关于C++11 使用带有自定义删除器的 unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486211/

相关文章:

c++ - C++ 是否有标准方法对两个 std::sets、 vector 等进行三态比较?

C++:调试时无法在 VS Code 中看到输出

c++ - 使用 `rdtsc` : error C2065

java - 在java中使用String来保存二进制数据是错误的吗?

c++ - 构造对象的构造函数参数访问成员字段

C++ 更快的字符数组比较

c++ - 在 Linux Mint 17.1 64 位(未声明/不是类型)上尝试 "make"C++ 项目时从 cstdlib 编译错误和类似错误

c++ - 如何在编译时确认自动推断类型的假设? (即 static_assert 样式

c++ - 用于复制/转换对象的一系列函数的模板

c++ - x = std::move(x) 未定义吗?