std::destroy

Header: <memory>

  1. 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

# 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);
}

# See also