std::destroy_at (3) Linux Manual Page
std::destroy_at – std::destroy_at
Synopsis
Defined in header<memory>
template <class T>
(since C++ 17)
void destroy_at(T *p);
Calls the destructor of the object pointed to by p, as if by p->~T().
Parameters
p – a pointer to the object to be destroyed
Return value
(none)
Possible implementation
Example
The following example demonstrates how to use destroy_at to destroy a contiguous sequence of elements.
// Run this code
#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);
}
Output:
See also
destroy destroys a range of objects
(C++17)
destroy_n destroys a number of objects in a range
(C++17)
