Namespaces
Variants

std::ranges:: destroy

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
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)
Definido en el encabezado <memory>
Firma de llamada
template < no-throw-input-iterator I, no - throw - sentinel - for < I > S >

requires std:: destructible < std:: iter_value_t < I >>

constexpr I destroy ( I first, S last ) noexcept ;
(1) (desde C++20)
template < no-throw-input-range R >

requires std:: destructible < ranges:: range_value_t < R >>

constexpr ranges:: borrowed_iterator_t < R > destroy ( R && r ) noexcept ;
(2) (desde C++20)
1) Destruye los objetos en el rango [ first , last ) , como si fuera mediante
for (; first != last; ++first)
    std::ranges::destroy_at(std::addressof(*first));
return first;
2) Igual que (1) , pero utiliza r como el rango fuente, como si se usara ranges:: begin ( r ) como first y ranges:: end ( r ) como last .

Las entidades similares a funciones descritas en esta página son algorithm function objects (conocidas informalmente como niebloids ), es decir:

Contenidos

Parámetros

first, last - el par iterador-centinela que define el rango de elementos a destruir
r - el range a destruir

Valor de retorno

Un iterador se compara igual a last .

Complejidad

Lineal en la distancia entre first y last .

Implementación posible

struct destroy_fn
{
    template<no-throw-input-iterator I, no-throw-sentinel-for<I> S>
        requires std::destructible<std::iter_value_t<I>>
    constexpr I operator()(I first, S last) const noexcept
    {
        for (; first != last; ++first)
            std::ranges::destroy_at(std::addressof(*first));
        return first;
    }
    template<no-throw-input-range R>
        requires std::destructible<std::ranges::range_value_t<R>>
    constexpr std::ranges::borrowed_iterator_t<R> operator()(R&& r) const noexcept
    {
        return operator()(std::ranges::begin(r), std::ranges::end(r));
    }
};
inline constexpr destroy_fn destroy{};

Ejemplo

El siguiente ejemplo demuestra cómo usar ranges::destroy para destruir una secuencia contigua de elementos.

#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));
    std::ranges::destroy(ptr, ptr + 8);
}

Salida:

0 destructed
1 destructed
2 destructed
3 destructed
4 destructed
5 destructed
6 destructed
7 destructed

Véase también

destruye una serie de objetos en un rango
(objeto función de algoritmo)
destruye un objeto en una dirección dada
(objeto función de algoritmo)
(C++17)
destruye un rango de objetos
(plantilla de función)