Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/ranges/destroy at"

From cppreference.com
< cpp‎ | memory
(~)
m ({{c}}, fmt)
 
Line 2: Line 2:
 
{{cpp/memory/navbar}}
 
{{cpp/memory/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | memory}}
+
{{dcl header|memory}}
{{dcl h | Call signature}}
+
{{dcl h|Call signature}}
{{dcl | since=c++20 |
+
{{dcl|since=c++20|
 
template< std::destructible T >
 
template< std::destructible T >
 
constexpr void destroy_at( T* p ) noexcept;
 
constexpr void destroy_at( T* p ) noexcept;
Line 10: Line 10:
 
{{dcl end}}
 
{{dcl end}}
  
If {{tt|T}} is not an array type, calls the destructor of the object pointed to by {{tt|p}}, as if by {{tt|p->~T()}}. Otherwise, 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 not an array type, calls the destructor of the object pointed to by {{c|p}}, as if by {{tt|p->~T()}}. Otherwise, recursively destroys elements of {{c|*p}} in order, as if by calling {{c|std::destroy(std::begin(*p), std::end(*p))}}.
  
 
{{cpp/ranges/niebloid}}
 
{{cpp/ranges/niebloid}}
Line 16: Line 16:
 
===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 23: Line 23:
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun | 1=
+
{{eq fun|1=
struct destroy_at_fn {
+
struct destroy_at_fn
  template<std::destructible T>
+
{
  constexpr void operator()(T *p) const noexcept
+
    template<std::destructible T>
  {
+
    constexpr void operator()(T *p) const noexcept
    if constexpr (std::is_array_v<T>)
+
    {
      for (auto &elem : *p)
+
        if constexpr (std::is_array_v<T>)
        operator()(std::addressof(elem));
+
            for (auto &elem : *p)
    else
+
                operator()(std::addressof(elem));
      p->~T();
+
        else
  }
+
            p->~T();
 +
    }
 
};
 
};
  
Line 42: Line 43:
 
{{tt|destroy_at}} deduces the type of object to be destroyed and hence avoids writing it explicitly in the destructor call.
 
{{tt|destroy_at}} deduces the type of object to be destroyed and hence avoids writing it explicitly in the destructor call.
  
When {{tt|destroy_at}} is called in the evaluation of some [[cpp/language/constant expression|constant expression]] {{c|e}}, the argument {{tt|p}} must point to an object whose lifetime began within the evaluation of {{c|e}}.
+
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}}.
  
 
===Example===
 
===Example===
Line 49: Line 50:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/ranges/dsc destroy}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy}}
{{dsc inc | cpp/memory/ranges/dsc destroy_n}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy_n}}
{{dsc inc | cpp/memory/ranges/dsc construct_at}}
+
{{dsc inc|cpp/memory/ranges/dsc construct_at}}
{{dsc inc | cpp/memory/dsc destroy_at}}
+
{{dsc inc|cpp/memory/dsc destroy_at}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|ja|ru|zh}}
 
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 07:41, 15 June 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(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)



 
Defined in header <memory>
Call signature
template< std::destructible T >
constexpr void destroy_at( T* p ) noexcept;
(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(). 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 niebloids, that is:

In practice, they may be implemented as function objects, or with special compiler extensions.

Contents

[edit] Parameters

p - a pointer to the object to be destroyed

[edit] Return value

(none)

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

#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

[edit] See also

destroys a range of objects
(niebloid)[edit]
destroys a number of objects in a range
(niebloid)[edit]
creates an object at a given address
(niebloid)[edit]
destroys an object at a given address
(function template) [edit]