std::ranges::destroy_at(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay so bestie, this is the std::ranges::destroy_at(3) page from the C++ Standard Libary manual, and it's literally here to explain destroying objects in the rangey way πŸ’…βœ¨

std::ranges::destroy_at β€” std::ranges::destroy_at

✨ Synopsis ✨

πŸ“ž Call signature πŸ“ž

Defined in header <memory>
Call signature
template< std::destructible T >              (since C++20)
constexpr void destroy_at( T* p ) noexcept;

If T is not an array type, it calls the destructor of the object pointed to by p, as if by p->~T(). Otherwise, it 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:

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

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

πŸŽ€ Parameters πŸŽ€

  • p β€” a pointer to the object to be destroyed πŸ’–

🌸 Return value 🌸

(none) β€” literally nothing comes back, periodt. 🫢

πŸ› οΈ Possible implementation πŸ› οΈ

here's the possible implementation, copied exactly so you can see the whole vibe:

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{};

πŸ“ Notes πŸ“

destroy_at deduces the type of object to be destroyed and hence avoids writing it explicitly in the destructor call. no cap, that's the whole point πŸ’…

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. that part is important, bestie ⚠️

πŸ§ͺ Example πŸ§ͺ

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

The page has the little // Run this code marker, so here's the 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

πŸ‘€ See also πŸ‘€

  • ranges::destroy β€” destroys a range of objects (C++20) (niebloid)
  • ranges::destroy_n β€” destroys a number of objects in a range (C++20) (niebloid)
  • ranges::construct_at β€” creates an object at a given address (C++20) (niebloid)
  • destroy_at β€” destroys an object at a given address (C++17) (function template)

xoxo, your Unix bestie πŸ’… β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::ranges::destroy_at(3) πŸŒˆπŸŽ€

πŸ“¦ where this page comes from