std::ranges::destroy - cppreference.com

From cppreference.com

Defined in header

<memory>

Call signature

template</*nothrow-input-iterator*/I,/*nothrow-sentinel-for*/<I>S>requiresstd::destructible<std::iter_value_t<I>>constexprIdestroy(Ifirst,Slast)noexcept; (1) (since C++20)template</*nothrow-input-range*/R>requiresstd::destructible<ranges::range_value_t<R>>constexprranges::borrowed_iterator_t<R>destroy(R&&r)noexcept; (2) (since C++20)template</*execution-policy*/Ep,/*nothrow-random-access-iterator*/I,/*nothrow-sized-sentinel-for*/<I>S>requiresstd::destructible<std::iter_value_t<I>>Idestroy(Ep&&policy,Ifirst,Slast); (3) (since C++26)template</*execution-policy*/Ep,/*nothrow-sized-random-access-range*/R>requiresstd::destructible<ranges::range_value_t<R>>ranges::borrowed_iterator_t<R>destroy(Ep&&policy,R&&r); (4) (since C++26)For the definition of /*execution-policy*/, see

this page

; for the definition of other exposition-only concepts, see

this page

.

1) Destroys elements in the target range [first, last) as if by

for(;first!=last;++first)std::ranges::destroy_at(std::addressof(*first));returnfirst;2) Same as (1), but uses r as the target range.

3,4) Same as (1,2), but executed according to policy.

The function-like entities described on this page are

algorithm function objects

(informally known as niebloids), that is:

Explicit template argument lists cannot be specified when calling any of them.

None of them are visible to

argument-dependent lookup

.

When any of them are found by

normal unqualified lookup

as the name to the left of the function-call operator,

argument-dependent lookup

is inhibited.

Parameters

first, last - the iterator-sentinel pair defining the

range

of elements to destroy r - the

range

to destroy policy - the

execution policy

to use Return value

As described above.

Exceptions

3,4) During the execution process:

If the temporary memory resources required for parallelization are not available,

std::bad_alloc

is thrown.

If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for

standard policies

,

std::terminate

is invoked).

Notes

Feature-test

macro ValueStdFeature

__cpp_lib_parallel_algorithm

202506L

(C++26)Parallel range algorithms Possible implementation

structdestroy_fn{template</*nothrow-input-iterator*/I,/*nothrow-sentinel-for*/<I>S>requiresstd::destructible<std::iter_value_t<I>>constexprIoperator()(Ifirst,Slast)constnoexcept{for(;first!=last;++first)ranges::destroy_at(std::addressof(*first));returnfirst;}template</*nothrow-input-range*/R>requiresstd::destructible<ranges::range_value_t<R>>constexprranges::borrowed_iterator_t<R>operator()(R&&r)constnoexcept{return(*this)(ranges::begin(r),ranges::end(r));}template</*nothrow-forward-range*/R>requiresstd::destructible<ranges::range_value_t<R>>constexprranges::borrowed_iterator_t<R>operator()(R&&r)constnoexcept{return(*this)(ranges::begin(r),ranges::next(ranges::begin(r),ranges::end(r)));}};inlineconstexprdestroy_fndestroy{};Example

Demonstrates how to use ranges::destroy to destroy a contiguous sequence of elements.

Run this code

#include<iostream>#include<memory>#include<new>structTracer{intvalue;~Tracer(){std::cout<<value<<" destructed\n";}};intmain(){alignas(Tracer)unsignedcharbuffer[sizeof(Tracer)*4];for(inti=0;i<4;++i)new(buffer+sizeof(Tracer)*i)Tracer{i};// manually construct objectsautoptr=std::launder(reinterpret_cast<Tracer*>(buffer));std::ranges::destroy(ptr,ptr+4);}Output:

0 destructed 1 destructed 2 destructed 3 destructed See also