std::default_delete
From cppreference.com
Defined in header
<memory>
|
||
template< class T > struct default_delete
|
(1) | (since C++11) |
template< class T > struct default_delete<T[]>
|
(2) | (since C++11) |
std::default_delete
is the default destruction policy used by std::unique_ptr when no deleter is specified.
1) The non-specialized default_delete
uses delete to deallocate memory for a single object.
2) A partial specialization for array types that uses delete[] is also provided.
Contents |
[edit] Member functions
(constructor) |
constructs a default_delete object (public member function) |
operator() |
deletes the object or array (public member function) |
std::default_delete::default_delete
constexpr default_delete() = default;
|
(1) | (since C++11) |
template <class U>
default_delete( const default_delete<U>& d ); |
(2) | (since C++11) (member only of generic default_delete template) |
1) Constructs a
std::default_delete
object.
2) Constructs a
std::default_delete
object from another std::default_delete
object. This constructor will only participate in overload resolution if U*
is implicitly convertible to T*
.Parameters
d | - | a deleter to copy from |
Exceptions
noexcept specification:
noexcept
Notes
The converting constructor template of std::default_delete
makes possible the implicit conversion from std::unique_ptr<Derived> to std::unique_ptr<Base>.
std::default_delete::operator()
void operator()(T* ptr) const;
|
(1) | (since C++11) |
template <class U>
void operator()(U* ptr) const = delete; |
(2) | (since C++11) (member only of default_delete<T[]> template specialization) |
Calls either delete
or delete[]
on ptr
.
Parameters
ptr | - | an object or array to delete |
Exceptions
No exception guarantees.
[edit] Example
Run this code
#include <memory> #include <vector> #include <algorithm> int main() { // { // std::shared_ptr<int> shared_bad(new int[10]); // } // the destructor calls delete, undefined behavior { std::shared_ptr<int> shared_good(new int[10], std::default_delete<int[]> ()); } // the destructor calls delete[], ok { std::unique_ptr<int> ptr(new int(5)); } // unique_ptr<int> uses default_delete<int> { std::unique_ptr<int[]> ptr(new int[10]); } // unique_ptr<int[]> uses default_delete<int[]> // default_delete can be used anywhere a delete functor is needed std::vector<int*> v; for(int n = 0; n < 100; ++n) v.push_back(new int(n)); std::for_each(v.begin(), v.end(), std::default_delete<int>()); }
[edit] See also
(C++11)
|
smart pointer with unique object ownership semantics (class template) |