|
|
Defined in header <memory> | ||
template<class ForwardIt> void destroy( ForwardIt first, ForwardIt last); | (1) | (since C++17) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt> void destroy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last); | (2) | (since C++17) |
[
first,
last)
, as if byfor(; first!= last;++first)std::destroy_at(std::addressof(*first));
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
Contents |
first, last | - | the pair of iterators defining therange of elements to destroy |
policy | - | theexecution policy to use |
Type requirements | ||
-ForwardIt must meet the requirements ofLegacyForwardIterator. | ||
-No increment, assignment, comparison, or indirection through valid instances ofForwardIt may throw exceptions. |
Linear in the distance betweenfirst andlast.
The overload with a template parameter namedExecutionPolicy
reports errors as follows:
ExecutionPolicy
is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy
, the behavior is implementation-defined.template<class ForwardIt>constexpr// since C++20void destroy(ForwardIt first, ForwardIt last){for(; first!= last;++first)std::destroy_at(std::addressof(*first));} |
The following example demonstrates how to usedestroy
to destroy a contiguous sequence of elements.
#include <iostream>#include <memory>#include <new> struct Tracer{int value; ~Tracer(){std::cout<< value<<" destructed\n";}}; int main(){ alignas(Tracer)unsignedchar buffer[sizeof(Tracer)*8]; for(int i=0; i!=8;++i) new(buffer+ sizeof(Tracer)* i) Tracer{i};// manually construct objects auto ptr=std::launder(reinterpret_cast<Tracer*>(buffer)); std::destroy(ptr, ptr+8);}
Output:
0 destructed1 destructed2 destructed3 destructed4 destructed5 destructed6 destructed7 destructed
(C++17) | destroys a number of objects in a range (function template)[edit] |
(C++17) | destroys an object at a given address (function template)[edit] |
(C++20) | destroys a range of objects (algorithm function object)[edit] |