坐怀不乱的大葱 · RPC error - Logic 2 ...· 2 周前 · |
兴奋的西装 · Drivers & Downloads - ...· 4 周前 · |
冷静的柑橘 · ZIGAO自高卫浴品牌简介,是几线品牌是德国 ...· 4 月前 · |
玉树临风的针织衫 · 舞出精彩 湘湘6 - 简舞秀· 9 月前 · |
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
.
default_delete
uses
delete
to deallocate memory for a single object.
Language support | ||||
Type support (basic types, RTTI) | ||||
Library feature-test macros (C++20) | ||||
Dynamic memory management | ||||
Program utilities | ||||
Coroutine support (C++20) | ||||
Variadic functions | ||||
(C++20)
|
||||
(C++26)
|
||||
(C++11)
|
||||
(C++20)
|
||||
Debugging support | ||||
(C++26)
|
||||
(C++26)
|
||||
(C++26)
|
||||
Three-way comparison | ||||
(C++20)
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
(C++20)
(C++20)
|
||||
(C++20)
(C++20)
(C++20)
|
||||
General utilities | ||||
Date and time | ||||
Function objects | ||||
Formatting library (C++20) | ||||
(C++11)
|
||||
Relational operators (deprecated in C++20) | ||||
Integer comparison functions | ||||
(C++20)
(C++20)
(C++20)
|
||||
(C++20)
(C++20)
(C++20)
|
||||
(C++20)
|
||||
Swap and type operations | ||||
(C++20)
|
||||
(C++14)
|
||||
(C++11)
|
||||
(C++23)
|
||||
(C++11)
|
||||
(C++23)
|
||||
(C++11)
|
||||
(C++11)
|
||||
(C++17)
|
||||
Common vocabulary types | ||||
(C++11)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++11)
|
||||
(C++11)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++23)
|
||||
Elementary string conversions | ||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++20)
|
||||
(C++11)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++20)
|
||||
Constrained uninitialized memory algorithms | ||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++20)
|
||||
Allocators | ||||
(C++11)
|
||||
(C++23)
|
||||
(C++11)
|
||||
(C++11)
|
||||
(C++20)
|
||||
(C++20)
|
||||
(C++11)
|
||||
(C++17)
|
||||
Memory resources | ||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
(C++17)
|
||||
Garbage collection support | ||||
(C++11)
(until C++23)
|
||||
(C++11)
(until C++23)
|
||||
(C++11)
(until C++23)
|
||||
(C++11)
(until C++23)
|
||||
(C++11)
(until C++23)
|
||||
(C++11)
(until C++23)
|
||||
Uninitialized storage | ||||
(
until C++20*
)
|
||||
(
until C++20*
)
|
||||
(
until C++20*
)
|
||||
Explicit lifetime management | ||||
(C++23)
(C++23)
|
std::default_delete
object.
std::default_delete
object. This constructor will only participate in overload resolution if
U
*
is implicitly convertible to
T
*
.
delete
on
ptr
.
In any case, if U is an incomplete type, the program is ill-formed.
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 C++ 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.
Contents |
(since C++11)
(until C++23) (member only of primary
default_delete
template)
|
template
<
class
U
>
constexpr default_delete ( const default_delete < U > & d ) noexcept ; |
(since C++23)
(member only of primary
default_delete
template)
|
(since C++11)
(until C++23) (member only of
default_delete<T[]>
specialization)
|
template
<
class
U
>
constexpr default_delete ( const default_delete < U [ ] > & d ) noexcept ; |
(since C++23)
(member only of
default_delete<T[]>
specialization)
|
void
operator
(
)
(
T
*
ptr
)
const
;
|
(since C++11)
(until C++23) (member only of primary
default_delete
template)
|
constexpr
void
operator
(
)
(
T
*
ptr
)
const
;
|
(since C++23)
(member only of primary
default_delete
template)
|
(since C++11)
(until C++23) (member only of
default_delete<T[]>
specialization)
|
template
<
class
U
>
constexpr void operator ( ) ( U * ptr ) const ; |
(since C++23)
(member only of
default_delete<T[]>
specialization)
|
Feature-test macro | Value | Feature |
__cpp_lib_constexpr_memory
|
202202L | (C++23) |
constexpr
constructor and
operator
(
)
[ edit ] Example
Run this code
#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[]>()); } // 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>()); |
Applied to | Behavior as published | Correct behavior | LWG 2118 | C++11 |
member functions of
default_delete<T[]>
rejected qualification conversions
|
accept
[ edit ] See also |
(C++11)
|
smart pointer with unique object ownership semantics
(class template) [edit] Toolbox
|
---|
玉树临风的针织衫 · 舞出精彩 湘湘6 - 简舞秀 9 月前 |