On this page
std::ranges::destroy
Defined in header <memory> |
||
---|---|---|
Call signature | ||
|
(1) | (since C++20) |
|
(2) | (since C++20) |
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 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 destroy |
r | - | the range to destroy |
Return value
An iterator compares equal to last
.
Complexity
Linear in the distance between first
and last
.
Possible implementation
|
Example
The following example demonstrates how to use ranges::destroy
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) 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
(C++20)
|
destroys a number of objects in a range (niebloid) |
(C++20)
|
destroys an object at a given address (niebloid) |
(C++17)
|
destroys a range of objects (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/memory/ranges/destroy