std::ranges::destroy_at
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
ranges::destroy_at
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)
(until C++20*)
Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
Defined in header
<memory>
Call signature
template< std::destructible T >
constexpr void destroy_at( T* p ) noexcept;
(since C++20)
constexpr void destroy_at( T* p ) noexcept;
If T
is not an array type, calls the destructor of the object pointed to by p, as if by p->~T(). Otherwise, recursively destroys elements of *p in order, as if by calling std::destroy (std::begin (*p), std::end (*p)).
The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
[edit] Parameters
p
-
a pointer to the object to be destroyed
[edit] Possible implementation
struct destroy_at_fn { template<std::destructible T> constexpr void operator()(T* p) const noexcept { if constexpr (std::is_array_v <T>) for (auto& elem : *p) operator()(std::addressof (elem)); else p->~T(); } }; inline constexpr destroy_at_fn destroy_at{};
[edit] Notes
destroy_at
deduces the type of object to be destroyed and hence avoids writing it explicitly in the destructor call.
When destroy_at
is called in the evaluation of some constant expression e, the argument p must point to an object whose lifetime began within the evaluation of e.
[edit] Example
The following example demonstrates how to use ranges::destroy_at
to destroy a contiguous sequence of elements.
Run this code
#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)); for (int i = 0; i != 8; ++i) std::ranges::destroy_at(ptr + i); }
Output:
0 destructed 1 destructed 2 destructed 3 destructed 4 destructed 5 destructed 6 destructed 7 destructed