std::destroy
Min standard notice:
Header: <memory>
- Destroys the objects in the range [first,last), as if by for (; first != last; ++first) std::destroy_at(std::addressof(*first));
# Declarations
template< class ForwardIt >
void destroy( ForwardIt first, ForwardIt last );
(since C++17) (until C++20)
template< class ForwardIt >
constexpr void destroy( ForwardIt first, ForwardIt last );
(since C++20)
template< class ExecutionPolicy, class ForwardIt >
void destroy( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last );
(since C++17)
# Parameters
first, last: the range of elements to destroypolicy: the execution policy to use
# Return value
(none)
# 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(ptr, ptr + 8);
}