最終更新日時(UTC):
が更新

履歴 編集

function
<memory>

std::indirect::代入演算子(C++26)

constexpr indirect& operator=(const indirect& other);              // (1)
constexpr indirect& operator=(indirect&& other) noexcept(see below); // (2)
template <class U = T>
constexpr indirect& operator=(U&& u);                              // (3)

概要

  • (1) : コピー代入。otherが所有するオブジェクトをディープコピーする。
  • (2) : ムーブ代入。otherから所有権を移す。
  • (3) : 所有するオブジェクトに値uを代入する。

テンプレートパラメータ制約

適格要件

効果

  • (1) : otherが無効値状態であれば*thisも無効値状態にする。そうでなければ、アロケータの伝播規則に従い、otherが所有するオブジェクトのコピーを*thisがもつようにする。
  • (2) : otherが無効値状態であれば*thisも無効値状態にする。そうでなければ、所有権を移すかムーブ構築する。otherは無効値状態となる。
  • (3) : *thisが無効値状態であればuからTを構築して所有する。そうでなければ**this = std::forward<U>(u)と等価。

戻り値

*thisへの参照。

例外

noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
         allocator_traits<Allocator>::is_always_equal::value)

#include <cassert>
#include <memory>

int main()
{
  std::indirect<int> a{1};
  std::indirect<int> b{2};

  a = b;                  // (1) コピー代入(ディープコピー)
  assert(*a == 2);
  *a = 3;
  assert(*b == 2);        // bは影響を受けない

  a = 10;                 // (3) 値の代入
  assert(*a == 10);

  a = std::move(b);       // (2) ムーブ代入
  assert(*a == 2);
  assert(b.valueless_after_move());
}

出力

バージョン

言語

  • C++26

処理系

関連項目

参照