名前空間
変種
操作

std::inplace_vector<T,N>::operator=

From cppreference.com
 
 
 
 
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 によって識別される内容で置き換えます。

目次

[編集] パラメータ

その他 - コンテナの要素を初期化するために使用される、別のinplace_vector
init - コンテナの要素を初期化するための初期化リスト

[編集] 計算量

1,2) *thisother のサイズに対して線形。
3) *thisinit のサイズに対して線形。

[編集] 例外

2)
noexcept 指定:  
noexcept(N == 0 ||

        (std::is_nothrow_move_assignable_v<T> &&

         std::is_nothrow_move_constructible_v<T>))
3) init.size() > N の場合、std::bad_alloc を送出します。

[編集]

#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を構築します
(公開メンバ関数) [編集]
コンテナに値を代入する
(公開メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)