std::inplace_vector<T,N>:: operator=
From cppreference.net
<
cpp
|
container
|
inplace vector
|
constexpr
inplace_vector
&
operator
=
(
const
inplace_vector
&
other
)
;
|
(1) | (C++26以降) |
|
constexpr
inplace_vector
&
operator
=
(
inplace_vector
&&
other
)
noexcept ( /* 下記参照 */ ) ; |
(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)
変更点:
- "specification:" → "noexcept指定:"
- HTMLタグ、属性、
noexcept
noexcept指定:
noexcept
(
N
==
0
||
(
std::
is_nothrow_move_assignable_v
<
T
>
&&
タグ内のC++コードは一切翻訳せず保持
- C++固有の用語(noexcept、std::is_nothrow_move_assignable_v、std::is_nothrow_move_constructible_vなど)は翻訳せず保持
- 元のフォーマットと構造を完全に維持
翻訳のポイント:
- "Throws" → 「例外」と訳し、自然な日本語の文脈に合わせて「がスローされる条件:」と補完
- HTMLタグ、属性、
タグ内のC++コードは完全に保持
- 技術文書としての正確性と専門性を維持
- 日本語として自然な語順と表現になるよう調整
例
このコードを実行
#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("初期状態:"); std::println("x = {}", x); std::println("y = {}", y); std::println("コピー代入によりxからyへデータをコピー:"); y = x; // オーバーロード (1) std::println("x = {}", x); std::println("y = {}", y); std::inplace_vector<std::string, 3> z, w{"\N{CAT}", "\N{GREEN HEART}"}; std::println("初期状態:"); std::println("z = {}", z); std::println("w = {}", w); std::println("ムーブ代入によりwからzへデータを移動:"); z = std::move(w); // オーバーロード (2) std::println("z = {}", z); std::println("w = {}", w); // wは有効だが未規定の状態 auto l = {4, 5, 6, 7}; std::println("initializer_list {} のxへの代入:", l); x = l; // オーバーロード (3) std::println("x = {}", x); std::println("Nより大きいサイズのinitializer_listの代入は例外を送出:"); try { x = {1, 2, 3, 4, 5}; // 送出: (初期化子リストサイズ == 5) > (容量 N == 4) } catch(const std::bad_alloc& ex) { std::println("ex.what(): {}", ex.what()); } }
出力例:
初期状態: x = [1, 2, 3] y = [] コピー代入によりxからyへデータをコピー: x = [1, 2, 3] y = [1, 2, 3] 初期状態: z = [] w = ["🐈", "💚"] ムーブ代入によりwからzへデータを移動: z = ["🐈", "💚"] w = ["", ""] initializer_list [4, 5, 6, 7] のxへの代入: x = [4, 5, 6, 7] Nより大きいサイズのinitializer_listの代入は例外を送出: ex.what(): std::bad_alloc
関連項目
inplace_vector
を構築する
(公開メンバ関数) |
|
|
コンテナに値を代入する
(公開メンバ関数) |