std::inplace_vector<T,N>::inplace_vector
From cppreference.com
constexpr inplace_vector() noexcept;
|
(1) | (since C++26) |
constexpr explicit inplace_vector( size_type count );
|
(2) | (since C++26) |
constexpr inplace_vector( size_type count, const T& value );
|
(3) | (since C++26) |
template< class InputIt >
constexpr inplace_vector( InputIt first, InputIt last );
|
(4) | (since C++26) |
template< /*container-compatible-range*/<T> R >
constexpr inplace_vector( std::from_range_t, R&& rg );
|
(5) | (since C++26) |
constexpr inplace_vector( const inplace_vector& other );
|
(6) | (since C++26) |
constexpr inplace_vector( inplace_vector&& other )
noexcept(N == 0 || std::is_nothrow_move_constructible_v<T>);
|
(7) | (since C++26) |
constexpr inplace_vector( std::initializer_list<T> init );
|
(8) | (since C++26) |
Constructs a new inplace_vector from a variety of data sources.
1) Constructs an empty
inplace_vector.3) Constructs an
inplace_vector with count copies of elements with value value.4) Constructs an
inplace_vector with the contents of the range [first, last).5) Constructs an
inplace_vector with the contents of the range rg. For the definition of
/*container-compatible-range*/, see ranges::to. The program is ill-formed if
ranges::size(rg) is a constant expression and is greater than N.6) Copy constructor. Constructs an
inplace_vector with the copy of the contents of other.7) Move constructor. Constructs an
inplace_vector with the contents of other using move semantics.8) Constructs an
inplace_vector with the contents of the initializer list init.Parameters
| count | - | the size of the container |
| value | - | the value to initialize elements of the container with |
| first, last | - | the pair of iterators defining the source range of elements to copy |
| rg | - | the range of values to initialize elements of the container with |
| other | - | another inplace_vector to be used as source to initialize the elements of the container with
|
| init | - | initializer list to initialize the elements of the container with |
| Type requirements | ||
-T must meet the requirements of DefaultInsertable in order to use overloads (2,3).
| ||
Complexity
1) Constant.
2,3) Linear in
count.4) Linear in
std::distance(first, last).5) Linear in
std::ranges::distance(rg).6,7) Linear in size of
other.8) Linear in size of
init.Exceptions
2,3) Throws std::bad_alloc if
count > N.4) Throws std::bad_alloc if
std::ranges::distance(first, last) > N.5) Throws std::bad_alloc if
std::ranges::distance(rg) > N.8) Throws std::bad_alloc if
init.size() > N.Example
Run this code
#include <cassert>
#include <initializer_list>
#include <inplace_vector>
#include <new>
#include <print>
#include <ranges>
int main()
{
std::inplace_vector<int, 4> v1; // overload (1)
assert(v1.size() == 0 && v1.capacity() == 4);
std::inplace_vector<int, 0> v2; // overload (1), N == 0 is allowed
assert(v2.size() == 0 && v2.capacity() == 0);
std::inplace_vector<int, 5> v3(3); // overload (2)
assert(v3.size() == 3 && v3.capacity() == 5);
std::println("v3 = {}", v3);
try
{
std::inplace_vector<int, 3> v(4); // overload (2), throws: count > N
}
catch(const std::bad_alloc& ex1)
{
std::println("ex1.what(): {}", ex1.what());
}
std::inplace_vector<int, 5> v4(3, 8); // overload (3)
assert(v4.size() == 3 && v4.capacity() == 5);
std::println("v4 = {}", v4);
try
{
std::inplace_vector<int, 3> v(4, 2); // overload (3), throws: count > N
}
catch(const std::bad_alloc& ex2)
{
std::println("ex2.what(): {}", ex2.what());
}
const auto init = {1, 2, 3};
std::inplace_vector<int, 4> v5(init.begin(), init.end()); // overload (4)
assert(v5.size() == 3 && v5.capacity() == 4);
std::println("v5 = {}", v5);
std::inplace_vector<int, 4> v6(std::from_range, init); // overload (5)
assert(v6.size() == 3 && v6.capacity() == 4);
std::println("v6 = {}", v6);
std::inplace_vector<int, 4> v7(v6); // overload (6)
assert(v7.size() == 3 && v7.capacity() == 4);
std::println("v7 = {}", v7);
assert(v6.size() == 3);
std::inplace_vector<int, 4> v8(std::move(v6)); // overload (7)
// Note that after the move v6 is left in valid but indeterminate state.
assert(v8.size() == 3 && v8.capacity() == 4);
std::println("v8 = {}", v8);
std::inplace_vector<int, 4> v9(init); // overload (8)
assert(v9.size() == 3 && v9.capacity() == 4);
std::println("v9 = {}", v9);
try
{
std::inplace_vector<int, 2> v(init); // overload (8), throws: init.size() > N
}
catch(const std::bad_alloc& ex3)
{
std::println("ex3.what(): {}", ex3.what());
}
}
Possible output:
v3 = [0, 0, 0]
ex1.what(): std::bad_alloc
v4 = [42, 42, 42]
ex2.what(): std::bad_alloc
v5 = [1, 2, 3]
v6 = [1, 2, 3]
v7 = [1, 2, 3]
v8 = [1, 2, 3]
v9 = [1, 2, 3]
ex3.what(): std::bad_alloc
See also
| assigns values to the container (public member function) | |
[static] |
returns the number of elements that can be held in currently allocated storage (public static member function) |
| direct access to the underlying contiguous storage (public member function) | |
| returns the number of elements (public member function) | |
(C++17)(C++20) |
returns the size of a container or array (function template) |
(C++17) |
obtains the pointer to the underlying array (function template) |