std::ranges::destroy() algorithm
- od C++20
- Simplified
- Detailed
// (1)
constexpr I destroy( I first, S last ) noexcept;
// (2)
constexpr ranges::borrowed_iterator_t<R> destroy( R&& r ) noexcept;
The type of arguments are generic and have following constraints:
I-no-throw-input-iteratorS-no-throw-sentinel-for<O>R-no-throw-input-range
Additionally, each overload has the following constraints:
- (1)
std::destructible<std::iter_value_t<I>> - (1)
std::destructible<ranges::range_value_t<R>>
// (1)
template< no-throw-input-iterator I, no-throw-sentinel-for<I> S
requires std::destructible<std::iter_value_t<I>>
constexpr I destroy( I first, S last ) noexcept;
// (2)
template< no-throw-input-range R >
requires std::destructible<ranges::range_value_t<R>>
constexpr ranges::borrowed_iterator_t<R> destroy( R&& r ) noexcept;
-
(1) Destroys the objects in the range [
first;last), as if byfor (; first != last; ++first)
std::ranges::destroy_at(std::addressof(*first));
return first;uwagaIf an exception is thrown during the initialization then the objects that already constructed in [
ofirst;olast) are destroyed in an unspecified order.Also, the objects in [
ifirst;ilast) that were already moved, are left in a valid but unspecified state. -
(2) Same as (1), but uses
in_rangeas the first range andout_rangeas the second range, as if usingranges::begin(in_range)asifirst,ranges::end(in_range)asilast,ranges::begin(out_range)asofirst, andranges::end(out_range)asolast.
The function-like entities described on this page are niebloids.
Parameters
first last | The range of elements to destroy. |
r | The range of elements to destroy. |
Return value
An iterator compares equal to last.
Complexity
Linear in the distance between first and last.
Exceptions
The exception thrown on construction of the elements in the destination range, if any.
Possible implementation
destroy(1) and destroy(2)
Examples
#include <iostream>
#include <memory>
#include <new>
struct Tracer
{
int value;
~Tracer() { std::cout << value << " destructed\n"; }
};
int main()
{
alignas(Tracer) unsigned char 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::ranges::destroy(ptr, ptr + 8);
}
0 destructed
1 destructed
2 destructed
3 destructed
4 destructed
5 destructed
6 destructed
7 destructed
Hover to see the original license.