std::inplace_vector<T,N>::operator=
来自 cppreference.com
constexpr inplace_vector& operator=( const inplace_vector& other ); |
(1) | (自 C++26 起) |
constexpr inplace_vector& operator=( inplace_vector&& other ) noexcept(/* see below */); |
(2) | (自 C++26 起) |
constexpr inplace_vector& operator=( std::initializer_list<T> init ); |
(3) | (自 C++26 起) |
替换 inplace_vector
的内容。
1) 复制赋值运算符。也是一个 平凡的复制赋值运算符,如果 std::inplace_vector<T, N> 具有 平凡的析构函数,并且 std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_assignable_v<T> 为 true。用 other 内容的副本替换内容。
2) 移动赋值运算符。也是一个 平凡的移动赋值运算符,如果 std::inplace_vector<T, N> 具有 平凡的析构函数,并且 std::is_trivially_move_constructible_v<T> && std::is_trivially_move_assignable_v<T> 为 true。使用移动语义(即,从 other 中移动 other 的数据到此容器)将内容替换为 other 的内容。之后,other 处于有效但未指定的状态。
3) 用由初始化列表 init 标识的内容替换内容。
内容 |
[编辑] 参数
other | - | 另一个 inplace_vector ,用作源来初始化容器的元素 |
init | - | 初始化列表,用于初始化容器的元素 |
[编辑] 复杂度
1,2) *this 和 other 的大小线性。
3) *this 和 init 的大小线性。
[编辑] 异常
2)
noexcept 规范:
noexcept(N == 0 ||
(std::is_nothrow_move_assignable_v<T> &&
[编辑] 示例
运行此代码
#include <initializer_list> #include <inplace_vector> #include <new> #include <print> #include <ranges> #include <string> int main() { std::inplace_vector<int, 4> x({1, 2, 3}), y; std::println("Initially:"); std::println("x = {}", x); std::println("y = {}", y); std::println("Copy assignment copies data from x to y:"); y = x; // overload (1) std::println("x = {}", x); std::println("y = {}", y); std::inplace_vector<std::string, 3> z, w{"\N{CAT}", "\N{GREEN HEART}"}; std::println("Initially:"); std::println("z = {}", z); std::println("w = {}", w); std::println("Move assignment moves data from w to z:"); z = std::move(w); // overload (2) std::println("z = {}", z); std::println("w = {}", w); // w is in valid but unspecified state auto l = {4, 5, 6, 7}; std::println("Assignment of initializer_list {} to x:", l); x = l; // overload (3) std::println("x = {}", x); std::println("Assignment of initializer_list with size bigger than N throws:"); try { x = {1, 2, 3, 4, 5}; // throws: (initializer list size == 5) > (capacity N == 4) } catch(const std::bad_alloc& ex) { std::println("ex.what(): {}", ex.what()); } }
可能的输出
Initially: x = [1, 2, 3] y = [] Copy assignment copies data from x to y: x = [1, 2, 3] y = [1, 2, 3] Initially: z = [] w = ["🐈", "💚"] Move assignment moves data from w to z: z = ["🐈", "💚"] w = ["", ""] Assignment of initializer_list [4, 5, 6, 7] to x: x = [4, 5, 6, 7] Assignment of initializer_list with size bigger than N throws: ex.what(): std::bad_alloc
[编辑] 另请参阅
构造 inplace_vector (公有成员函数) | |
将值分配给容器 (公有成员函数) |