std::destroy_n
Min standard notice:
Header: <memory>
- Destroys the n objects in the range starting at first, as if by for (; n > 0; (void) ++first, –n) std::destroy_at(std::addressof(*first));
# Declarations
template< class ForwardIt, class Size >
ForwardIt destroy_n( ForwardIt first, Size n );
(since C++17) (until C++20)
template< class ForwardIt, class Size >
constexpr ForwardIt destroy_n( ForwardIt first, Size n );
(since C++20)
template< class ExecutionPolicy, class ForwardIt, class Size >
ForwardIt destroy_n( ExecutionPolicy&& policy, ForwardIt first, Size n );
(since C++17)
# Parameters
first: the beginning of the range of elements to destroyn: the number of elements to destroypolicy: the execution policy to use
# Return value
The end of the range of objects that has been destroyed (i.e., std::next(first, n)).
# Example
#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::destroy_n(ptr, 8);
}