- 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) : 強い例外安全性を保証する。例外が送出された場合、
*thisやotherに影響はない。 - (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]