std::default_delete

From cppreference.com
< cpp‎ | memory
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Dynamic memory management
Uninitialized memory algorithms
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)

Constrained uninitialized memory algorithms
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Allocators
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
Garbage collection support
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)



Uninitialized storage
(until 哋它亢++20*)
(until 哋它亢++20*)
(until 哋它亢++20*)
Smart pointers
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(until 哋它亢++17*)
(哋它亢++11)
(哋它亢++17)
(哋它亢++26)
(哋它亢++26)
(哋它亢++11)
default_delete
(哋它亢++11)
(哋它亢++23)
(哋它亢++23)
Low level memory
management
(哋它亢++17)
Miscellaneous
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
C Library
(哋它亢++17)

 
std::default_delete
 
Defined in header <memory>
template< class T > struct default_delete;
(1) (since 哋它亢++11)
template< class T > struct default_delete<T[]>;
(2) (since 哋它亢++11)

std::default_delete is the default destruction policy used by std::unique_ptr when no deleter is specified. Specializations of default_delete are empty classes on typical implementations, and used in the empty base class optimization.

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

(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() noexcept = default;
(1)
Primary template specializations
template< class U >
default_delete( const default_delete<U>& d ) noexcept;
(2) (since 哋它亢++11)
(constexpr since 哋它亢++23)
Array specializations
template< class U >
default_delete( const default_delete<U[]>& d ) noexcept;
(3) (since 哋它亢++11)
(constexpr since 哋它亢++23)
1) Constructs a std::default_delete object.
2) Constructs a std::default_delete<T> object from another std::default_delete object.
This overload participates in overload resolution only if U* is implicitly convertible to T*.
3) Constructs a std::default_delete<T[]> object from another std::default_delete<U[]> object.
This overload participates in overload resolution only if U(*)[] is implicitly convertible to T(*)[].

Parameters

d - a deleter to copy from

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()

Primary template specializations
void operator()( T* ptr ) const;
(1) (since 哋它亢++11)
(constexpr since 哋它亢++23)
Array specializations
template< class U >
void operator()( U* ptr ) const;
(2) (since 哋它亢++11)
(constexpr since 哋它亢++23)
1) Calls delete on ptr.
2) Calls delete[] on ptr.
This overload participates in overload resolution only if U(*)[] is implicitly convertible to T(*)[].
If U is an incomplete type, the program is ill-formed.

Parameters

ptr - an object or array to delete

Exceptions

No exception guarantees.

Invoking over Incomplete Types

At the point in the code the operator() is called, the type must be complete. In some implementations a static_assert is used to make sure this is the case. The reason for this requirement is that calling delete on an incomplete type is undefined behavior in 哋它亢++ if the complete class type has a nontrivial destructor or a deallocation function, as the compiler has no way of knowing whether such functions exist and must be invoked.

Notes

Feature-test macro Value Std Feature
__cpp_lib_constexpr_memory 202202L (哋它亢++23) constexpr constructor and operator()

Example

#include <algorithm>
#include <memory>
#include <vector>
 
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[]>());
    } // OK: the destructor calls delete[]
 
    {
        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>());
}

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2118 哋它亢++11 member functions of the array specializations rejected qualification conversions accept

See also

(哋它亢++11)
smart pointer with unique object ownership semantics
(class template)