std::ranges::destroy(3)
π std::ranges::destroy π
okay so this is std::ranges::destroy(3) from the C++ Standard Libary manual β manual name folded in near the top, spelling exactly the way the header gives it. bestie, we are destroying objects in a range, and the range-vibes are immaculate β¨
β¨ NAME β¨
std::ranges::destroy β std::ranges::destroy
(yes, the NAME section is literally serving the same name twice, and honestly? iconic π )
π Synopsis π
Defined in header <memory>.
π Call signature π
template< no-throw-input-iterator I, no-throw-sentinel-for<I> S >
requires std::destructible<std::iter_value_t<I>>
(1) (since C++20)
constexpr I destroy( I first, S last ) noexcept;
template< no-throw-input-range R >
requires std::destructible<ranges::range_value_t<R>>
(2) (since C++20)
constexpr ranges::borrowed_iterator_t<R> destroy( R&& r )
noexcept;
- Destroys the objects in the range
[first, last), as if by
for (; first != last; ++first)
std::ranges::destroy_at(std::addressof(*first));
return first;
- Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
The function-like entities described on this page are 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.
In practice, they may be implemented as function objects, or with special compiler extensions.
π§ Parameters π§
first,lastβ iterator-sentinel pair denoting the range of elements to destroyrβ the range to destroy
π Return value π
An iterator compares equal to last.
π Complexity π
Linear in the distance between first and last.
π οΈ 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{};
πΈ Example πΈ
The following example demonstrates how to use ranges::destroy to destroy a contiguous sequence of elements.
The original page has this little button text: // 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
π See also π
ranges::destroy_nβ destroys a number of objects in a range (C++20) (niebloid)ranges::destroy_atβ destroys an object at a given address (C++20) (niebloid)destroyβ destroys a range of objects (C++17) (function template)
mwah π β footer tea, folded into a cute sign-off: source http://cppreference.com, version/date 2024.06.10, page std::ranges::destroy(3). periodt π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms