Namespaces
Variants
Views
Actions

std::default_delete

From cppreference.com
< cpp‎ | memory
Revision as of 08:07, 14 November 2012 by Nate (Talk | contribs)

 
 
 
 
std::default_delete
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <memory>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class T > struct default_delete
</td>

<td > (1) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl ">

<td >
template< class T > struct default_delete<T[]>
</td>

<td > (2) </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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.

Member functions

constructs a default_delete object
(public member function)
deletes the object or array
(public member function)

Example

#include <memory>
 
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[]>
}


See also

Template:cpp/memory/dcl list unique ptr