std::inplace_vector<T,N>::assign_range
From cppreference.com
< cpp | container | inplace vector
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::inplace_vector
inplace_vector::assign_range
template< container-compatible-range <T> R >
constexpr void assign_range( R&& rg );
(since C++26)
constexpr void assign_range( R&& rg );
Replaces elements in the container with a copy of each element in rg.
This section is incomplete
Each iterator in the range rg is dereferenced exactly once.
If rg overlaps with *this, the behavior is undefined.
Contents
[edit] Parameters
rg
-
an
input_range
with reference type convertible to the element type of the container
Type requirements
-If std::assignable_from <T&, ranges::range_reference_t <R>> is not modeled, the program is ill-formed.
-If
T
is not EmplaceConstructible into inplace_vector
from *ranges::begin (rg), the behavior is undefined.
Exceptions
- std::bad_alloc , if std::ranges::distance (rg) > capacity().
- Any exception thrown by initialization of inserted element.
[edit] Example
Run this code
#include <algorithm> #include <cassert> #include <initializer_list> #include <inplace_vector> #include <iostream> #include <new> int main() { const auto source = {1, 2, 3}; std::inplace_vector <int, 4> destination{4, 5}; destination.assign_range(source); assert (std::ranges::equal (destination, source)); try { const auto bad = {-1, -2, -3, -4, -5}; destination.assign_range(bad); // throws: bad.size() > destination.capacity() } catch(const std::bad_alloc & ex) { std::cout << ex.what() << '\n'; } }
Possible output:
std::bad_alloc