Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory
(niebloid)
m (See also)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{cpp/ranges/title|destroy}}
+
{{cpp/ranges/title|destroy_n}}
 
{{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 |
+
{{dcl|since=c++20|
 
template< no-throw-input-iterator I >
 
template< no-throw-input-iterator I >
 
     requires std::destructible<std::iter_value_t<I>>
 
     requires std::destructible<std::iter_value_t<I>>
Line 11: Line 11:
 
{{dcl end}}
 
{{dcl end}}
  
Destroys the {{tt|n}} objects in the range starting at {{tt|first}}, equivalent to
+
Destroys the {{c|n}} objects in the range starting at {{c|first}}, equivalent to
 
{{source|1=
 
{{source|1=
 
return std::ranges::destroy(std::counted_iterator(first, n), std::default_sentinel).base();
 
return std::ranges::destroy(std::counted_iterator(first, n), std::default_sentinel).base();
Line 20: Line 20:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | first | the beginning of the range of elements to destroy}}
+
{{par|first|the beginning of the range of elements to destroy}}
{{par | n | the number of elements to destroy}}
+
{{par|n|the number of elements to destroy}}
 
{{par end}}
 
{{par end}}
  
Line 28: Line 28:
  
 
===Complexity===
 
===Complexity===
Linear in {{tt|n}}.
+
Linear in {{c|n}}.
  
 
===Possible implementation===
 
===Possible implementation===
{{eq fun | 1=
+
{{eq fun|1=
struct destroy_n_fn {
+
struct destroy_n_fn
  template</*no-throw-input-iterator*/ I>
+
{
    requires std::destructible<std::iter_value_t<I>>
+
    template<no-throw-input-iterator I>
  constexpr I operator()(I first, std::iter_difference_t<I> n) const noexcept
+
        requires std::destructible<std::iter_value_t<I>>
  {
+
    constexpr I operator()(I first, std::iter_difference_t<I> n) const noexcept
    for (; n != 0; (void)++first, --n)
+
    {
      std::ranges::destroy_at(std::addressof(*first));
+
        for (; n != 0; (void)++first, --n)
    return first;
+
            std::ranges::destroy_at(std::addressof(*first));
  }
+
        return first;
 +
    }
 
};
 
};
  
Line 51: Line 52:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/ranges/dsc destroy_at}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy_at}}
{{dsc inc | cpp/memory/ranges/dsc destroy}}
+
{{dsc inc|cpp/memory/ranges/dsc destroy}}
{{dsc inc | cpp/memory/dsc destroy_n}}
+
{{dsc inc|cpp/memory/dsc destroy_n}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|es|ja|zh}}
+
{{langlinks|es|ja|ru|zh}}

Latest revision as of 21:31, 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 >

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

constexpr I destroy_n( I first, std::iter_difference_t<I> n ) noexcept;
(since C++20)

Destroys the n objects in the range starting at first, equivalent to

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 - the beginning of the range of elements to destroy
n - the number of elements to destroy

[edit] Return value

The end of the range of objects that has been destroyed.

[edit] Complexity

Linear in n.

[edit] Possible implementation

struct destroy_n_fn
{
    template<no-throw-input-iterator I>
        requires std::destructible<std::iter_value_t<I>>
    constexpr I operator()(I first, std::iter_difference_t<I> n) const noexcept
    {
        for (; n != 0; (void)++first, --n)
            std::ranges::destroy_at(std::addressof(*first));
        return first;
    }
};
 
inline constexpr destroy_n_fn destroy_n{};

[edit] Example

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

Output:

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

[edit] See also

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