Defined in header <memory>
template< class T >
void destroy_at( T* p );
(since C++17)

Calls the destructor of the object pointed to by p, as if by p->~T().


p - a pointer to the object to be destroyed

Return value


Possible implementation

template<class T>
void destroy_at(T* p) 


The following example demonstrates how to use destroy_at to destroy a contiguous sequence of elements.

#include <memory>
#include <new>
#include <iostream>
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));
    for (int i = 0; i < 8; ++i)
        std::destroy_at(ptr + i);


0 destructed
1 destructed
2 destructed
3 destructed
4 destructed
5 destructed
6 destructed
7 destructed

See also

destroys a range of objects
(function template)
destroys a number of objects in a range
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.