std::ranges::destroy
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
ranges::destroy
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)
(until C++20*)
Garbage collector support (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)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
Defined in header
<memory>
Call signature
template< no-throw-input-iterator I, no-throw-sentinel-for<I> S >
(1)
(since C++20)
requires std::destructible <std::iter_value_t <I>>
template< no-throw-input-range R >
(2)
(since C++20)
requires std::destructible <ranges::range_value_t <R>>
1) Destroys the objects in the range
[
first,
last)
, as if by
for (; first != last; ++first) std::ranges::destroy_at (std::addressof (*first)); return first;
2) Same as (1), but uses r as the source range, as if using ranges::begin (r) as first and ranges::end (r) as last.
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.
[edit] Parameters
first, last
-
the iterator-sentinel pair defining the range of elements to destroy
r
-
the
range
to destroy
[edit] Return value
An iterator compares equal to last.
[edit] Complexity
Linear in the distance between first and last.
[edit] Possible implementation
struct destroy_fn { template<no-throw-input-iterator I, no-throw-sentinel-for<I> S> requires std::destructible <std::iter_value_t <I>> constexpr I operator()(I first, S last) const noexcept { for (; first != last; ++first) std::ranges::destroy_at (std::addressof (*first)); return first; } template<no-throw-input-range R> requires std::destructible <std::ranges::range_value_t <R>> constexpr std::ranges::borrowed_iterator_t <R> operator()(R&& r) const noexcept { return operator()(std::ranges::begin (r), std::ranges::end (r)); } }; inline constexpr destroy_fn destroy{};
[edit] Example
The following example demonstrates how to use ranges::destroy
to destroy a contiguous sequence of elements.
Run this code
#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); }
Output:
0 destructed 1 destructed 2 destructed 3 destructed 4 destructed 5 destructed 6 destructed 7 destructed