Difference between revisions of "cpp/memory/destroy at"
From cppreference.com
(CWG 2490 (no magic for destroy_at/ranges::destroy_at now)) |
Andreas Krug (Talk | contribs) m ({{c}}) |
||
Line 2: | Line 2: | ||
{{cpp/memory/navbar}} | {{cpp/memory/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | memory}} | + | {{dcl header|memory}} |
{{dcl rev multi | {{dcl rev multi | ||
− | | since1=c++17 | dcl1= | + | |since1=c++17|dcl1= |
template< class T > | template< class T > | ||
void destroy_at( T* p ); | void destroy_at( T* p ); | ||
− | | since2=c++20 | dcl2= | + | |since2=c++20|dcl2= |
template< class T > | template< class T > | ||
constexpr void destroy_at( T* p ); | constexpr void destroy_at( T* p ); | ||
Line 13: | Line 13: | ||
{{dcl end}} | {{dcl end}} | ||
− | If {{tt|T}} is not an array type, calls the destructor of the object pointed to by {{ | + | If {{tt|T}} is not an array type, calls the destructor of the object pointed to by {{c|p}}, as if by {{c|p->~T()}}. |
If {{tt|T}} is an array type, {{rev inl|until=c++20|the program is ill-formed}}{{rev inl|since=c++20|recursively destroys elements of {{c|*p}} in order, as if by calling {{c|std::destroy(std::begin(*p), std::end(*p))}}}}. | If {{tt|T}} is an array type, {{rev inl|until=c++20|the program is ill-formed}}{{rev inl|since=c++20|recursively destroys elements of {{c|*p}} in order, as if by calling {{c|std::destroy(std::begin(*p), std::end(*p))}}}}. | ||
Line 19: | Line 19: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | p | a pointer to the object to be destroyed}} | + | {{par|p|a pointer to the object to be destroyed}} |
{{par end}} | {{par end}} | ||
Line 26: | Line 26: | ||
===Possible implementation=== | ===Possible implementation=== | ||
− | {{eq fun | 1= | + | {{eq fun|1= |
template<class T> | template<class T> | ||
constexpr void destroy_at(T* p) | constexpr void destroy_at(T* p) | ||
Line 44: | Line 44: | ||
{{rrev|since=c++20| | {{rrev|since=c++20| | ||
− | When {{tt|destroy_at}} is called in the evaluation of some [[cpp/language/constant expression|constant expression]] {{c|e}}, the argument {{ | + | When {{tt|destroy_at}} is called in the evaluation of some [[cpp/language/constant expression|constant expression]] {{c|e}}, the argument {{c|p}} must point to an object whose lifetime began within the evaluation of {{c|e}}. |
}} | }} | ||
Line 52: | Line 52: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/memory/dsc destroy}} | + | {{dsc inc|cpp/memory/dsc destroy}} |
− | {{dsc inc | cpp/memory/dsc destroy_n}} | + | {{dsc inc|cpp/memory/dsc destroy_n}} |
− | {{dsc inc | cpp/memory/dsc construct_at}} | + | {{dsc inc|cpp/memory/dsc construct_at}} |
− | {{dsc inc | cpp/memory/ranges/dsc destroy_at}} | + | {{dsc inc|cpp/memory/ranges/dsc destroy_at}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|ja|ru|zh}} | {{langlinks|de|es|ja|ru|zh}} |
Latest revision as of 09:44, 13 June 2023
Defined in header <memory>
|
||
template< class T > void destroy_at( T* p ); |
(since C++17) (until C++20) |
|
template< class T > constexpr void destroy_at( T* p ); |
(since C++20) | |
If T
is not an array type, calls the destructor of the object pointed to by p, as if by p->~T().
If T
is an array type, the program is ill-formed(until C++20)recursively destroys elements of *p in order, as if by calling std::destroy(std::begin(*p), std::end(*p))(since C++20).
Contents |
[edit] Parameters
p | - | a pointer to the object to be destroyed |
[edit] Return value
(none)
[edit] Possible implementation
template<class T> constexpr void destroy_at(T* p) { if constexpr (std::is_array_v<T>) for (auto &elem : *p) (destroy_at)(std::addressof(elem)); else p->~T(); } // C++17 version: // template<class T> void destroy_at(T* p) { p->~T(); } |
[edit] Notes
destroy_at
deduces the type of object to be destroyed and hence avoids writing it explicitly in the destructor call.
When |
(since C++20) |
[edit] Example
The following example demonstrates how to use 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::destroy_at(ptr + i); }
Output:
0 destructed 1 destructed 2 destructed 3 destructed 4 destructed 5 destructed 6 destructed 7 destructed
[edit] See also
(C++17) |
destroys a range of objects (function template) |
(C++17) |
destroys a number of objects in a range (function template) |
(C++20) |
creates an object at a given address (function template) |
(C++20) |
destroys an object at a given address (niebloid) |