|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
unique_ptr::~unique_ptr | ||||
| Modifiers | ||||
| Observers | ||||
| Non-member functions | ||||
(C++14)(C++20) | ||||
(until C++20)(C++20) | ||||
(C++20) | ||||
| Helper classes | ||||
~unique_ptr(); | (since C++11) (constexpr since C++23) | |
Ifget()==nullptr there are no effects. Otherwise, the owned object is destroyed viaget_deleter()(get()).
Requires thatget_deleter()(get()) does not throw exceptions.
Althoughstd::unique_ptr<T> with the default deleter may be constructed withincomplete typeT, the typeT must be complete at the point of code where the destructor is called.
The following program demonstrates usage of a custom deleter.
#include <iostream>#include <memory> int main(){auto deleter=[](int* ptr){std::cout<<"[deleter called]\n"; delete ptr;}; std::unique_ptr<int, decltype(deleter)> uniq(newint, deleter);std::cout<<(uniq?"not empty\n":"empty\n"); uniq.reset();std::cout<<(uniq?"not empty\n":"empty\n");}
Output:
not empty[deleter called]empty