Skip to content

Latest commit

 

History

History
93 lines (68 loc) · 2.61 KB

File metadata and controls

93 lines (68 loc) · 2.61 KB

代入演算子

  • memory[meta header]
  • std[meta namespace]
  • polymorphic[meta class]
  • function[meta id-type]
  • cpp26[meta cpp]
constexpr polymorphic& operator=(const polymorphic& other);                // (1)
constexpr polymorphic& operator=(polymorphic&& other) noexcept(see below); // (2)

概要

  • (1) : コピー代入。otherが保持する派生型のオブジェクトをディープコピーする。
  • (2) : ムーブ代入。otherから所有権を移す。

適格要件

  • (1) : Tは完全型であること。

効果

  • (1) : addressof(other) == thisであれば何もしない。そうでなければ、アロケータの伝播規則に従い、otherが保持するオブジェクトのコピーを*thisがもつようにする。otherが無効値状態であれば*thisも無効値状態にする。
  • (2) : addressof(other) == thisであれば何もしない。そうでなければ、アロケータの伝播規則に従い、otherから所有権を移すか、ムーブ構築する。

戻り値

*thisへの参照。

例外

  • (1) : 強い例外安全性を保証する。例外が送出された場合、*thisotherに影響はない。
  • (2) : 以下と等価なnoexcept指定を持つ:
noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
         allocator_traits<Allocator>::is_always_equal::value)
  • allocator_traits[link /reference/memory/allocator_traits.md]

#include <cassert>
#include <memory>

struct Base {
  virtual ~Base() = default;
  virtual int f() const { return 0; }
};
struct Derived : Base {
  int x = 42;
  Derived() = default;
  Derived(int x) : x(x) {}
  int f() const override { return x; }
};

int main()
{
  std::polymorphic<Base> a{std::in_place_type<Derived>, 1};
  std::polymorphic<Base> b{std::in_place_type<Derived>, 2};

  a = b;             // (1) コピー代入(派生型のディープコピー)
  assert(a->f() == 2);

  a = std::move(b);  // (2) ムーブ代入
  assert(a->f() == 2);
}
  • std::polymorphic[color ff0000]
  • std::in_place_type[link /reference/utility/in_place_type_t.md]

出力

バージョン

言語

  • C++26

処理系

  • Clang: 22 [mark noimpl]
  • GCC: 16.1 [mark verified]
  • Visual C++: 2026 Update 2 [mark noimpl]

関連項目

参照