Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory
m (Possible implementation)
m (See also)
 
(One intermediate revision by one user not shown)
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 | num=1 |
+
{{dcl|since=c++20|num=1|
 
template< no-throw-input-iterator I, no-throw-sentinel-for<I> S >
 
template< no-throw-input-iterator I, no-throw-sentinel-for<I> S >
 
     requires std::destructible<std::iter_value_t<I>>
 
     requires std::destructible<std::iter_value_t<I>>
 
constexpr I destroy( I first, S last ) noexcept;
 
constexpr I destroy( I first, S last ) noexcept;
 
}}
 
}}
{{dcl | since=c++20 | num=2 |
+
{{dcl|since=c++20|num=2|
 
template< no-throw-input-range R >
 
template< no-throw-input-range R >
 
     requires std::destructible<ranges::range_value_t<R>>
 
     requires std::destructible<ranges::range_value_t<R>>
Line 16: Line 16:
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Destroys the objects in the range {{tt|[first, last)}}, as if by
+
@1@ Destroys the objects in the range {{range|first|last}}, as if by
 
{{source|1=
 
{{source|1=
 
for (; first != last; ++first)
 
for (; first != last; ++first)
Line 23: Line 23:
 
}}
 
}}
  
@2@ Same as {{v|1}}, but uses {{tt|r}} as the source range, as if using {{c|ranges::begin(r)}} as {{tt|first}} and {{c|ranges::end(r)}} as {{tt|last}}.
+
@2@ Same as {{v|1}}, but uses {{c|r}} as the source range, as if using {{c|ranges::begin(r)}} as {{c|first}} and {{c|ranges::end(r)}} as {{c|last}}.
  
 
{{cpp/ranges/niebloid}}
 
{{cpp/ranges/niebloid}}
Line 29: Line 29:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first, last | iterator-sentinel pair denoting the range of elements to destroy}}
+
{{par|first, last|iterator-sentinel pair denoting the range of elements to destroy}}
{{par | r | the range to destroy}}
+
{{par|r|the range to destroy}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
An iterator compares equal to {{tt|last}}.
+
An iterator compares equal to {{c|last}}.
  
 
===Complexity===
 
===Complexity===
Linear in the distance between {{tt|first}} and {{tt|last}}.
+
Linear in the distance between {{c|first}} and {{c|last}}.
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun | 1=
+
{{eq fun|1=
struct destroy_fn {
+
struct destroy_fn
  template<no-throw-input-iterator I, no-throw-sentinel-for<I> S>
+
{
    requires std::destructible<std::iter_value_t<I>>
+
    template<no-throw-input-iterator I, no-throw-sentinel-for<I> S>
  constexpr I operator()(I first, S last) const noexcept
+
        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));
+
        for (; first != last; ++first)
    return first;
+
            std::ranges::destroy_at(std::addressof(*first));
  }
+
        return first;
 +
    }
  
  template<no-throw-input-range R>
+
    template<no-throw-input-range R>
    requires std::destructible<std::ranges::range_value_t<R>>
+
        requires std::destructible<std::ranges::range_value_t<R>>
  constexpr std::ranges::borrowed_iterator_t<R> operator()(R&& r) const noexcept
+
    constexpr std::ranges::borrowed_iterator_t<R> operator()(R&& r) const noexcept
  {
+
    {
    return operator()(std::ranges::begin(r), std::ranges::end(r));
+
        return operator()(std::ranges::begin(r), std::ranges::end(r));
  }
+
    }
 
};
 
};
  
Line 67: Line 68:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/ranges/dsc destroy_n}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy_n}}
{{dsc inc | cpp/memory/ranges/dsc destroy_at}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy_at}}
{{dsc inc | cpp/memory/dsc destroy}}
+
{{dsc inc|cpp/memory/dsc destroy}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|es|ja|zh}}
+
{{langlinks|es|ja|ru|zh}}

Latest revision as of 21:24, 13 July 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< 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) (since 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) (since C++20)
1) Destroys the objects in the range [firstlast), as if by
for (; first != last; ++first)
    std::ranges::destroy_at(std::addressof(*first));
return first;
2) Same as (1), but uses r as the source range, as if using ranges::begin(r) as first and ranges::end(r) as last.

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

first, last - iterator-sentinel pair denoting the range of elements to destroy
r - the range to destroy

[edit] Return value

An iterator compares equal to last.

[edit] Complexity

Linear in the distance between first and last.

[edit] Possible implementation

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

[edit] Example

The following example demonstrates how to use ranges::destroy 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));
 
    std::ranges::destroy(ptr, ptr + 8);
}

Output:

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

[edit] See also

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