public member function
<memory>

std::unique_ptr::~unique_ptr

~unique_ptr();
销毁 unique_ptr
如果对象是空的 unique_ptr (即,get()==nullptr),则析构函数没有效果。

否则,它将删除其拥有的对象,就好像调用了get_deleter()(get())

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// unique_ptr destructor example
#include <iostream>
#include <memory>

int main () {
  auto deleter = [](int*p){
    delete p;
    std::cout << "[deleter called]\n";
  };

  std::unique_ptr<int,decltype(deleter)> foo (new int,deleter);

  std::cout << "foo " << (foo?"is not":"is") << " empty\n";

  return 0;                        // [deleter called]
}

输出
foo is not empty
[deleter called]


另见