Difference between revisions of "cpp/memory/ranges/destroy n"
From cppreference.com
(niebloid) |
m (→See also) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/ranges/title| | + | {{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 {{ | + | 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 {{ | + | 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>> | |
− | + | 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; | |
+ | } | ||
}; | }; | ||
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
Defined in header <memory>
|
||
Call signature |
||
template< no-throw-input-iterator I > requires std::destructible<std::iter_value_t<I>> |
(since C++20) | |
Destroys the n objects in the range starting at first, equivalent to
return std::ranges::destroy(std::counted_iterator(first, n), std::default_sentinel).base();
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.
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.
Run this 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)); 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
(C++20) |
destroys an object at a given address (niebloid) |
(C++20) |
destroys a range of objects (niebloid) |
(C++17) |
destroys a number of objects in a range (function template) |