std::polymorphic<T, Allocator>::operator= - cppreference.com (original) (raw)

constexpr polymorphic& operator=( const polymorphic& other ); (1) (since C++26)
constexpr polymorphic& operator=( polymorphic&& other ) noexcept(/* see below */); (2) (since C++26)

Replaces contents of *this with the contents of other.

Let traits be std::allocator_traits<Allocator>:

  1. If std::addressof(other) == this is true, does nothing. Otherwise, let need_update be traits::propagate_on_container_copy_assignment::value:
  1. If other is valueless, proceeds to the next step. Otherwise, constructs a new owned object in *this using traits::construct with *other as the argument, using the allocator update_alloc ? other._[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_ : _[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_.
  2. The previously owned object in *this (if any) is destroyed using traits::destroy and then the storage is deallocated.

After updating the object owned by *this, if need_update is true, _[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_ is replaced with a copy of other._[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_.

  1. If std::addressof(other) == this is true, does nothing. Otherwise, let need_update be traits::propagate_on_container_move_assignment::value:
  1. If other is valueless, proceeds to the next step. Otherwise, constructs a new owned object in *this using traits::construct with std::move(*other) as the argument, using the allocator update_alloc ? other._[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_ : _[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_.
  2. The previously owned object in *this (if any) is destroyed using traits::destroy and then the storage is deallocated.

After updating the objects owned by *this and other, if need_update is true, _[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_ is replaced with a copy of other._[alloc](../polymorphic.html#alloc "cpp/memory/polymorphic")_.

If all following conditions are satisfied, the program is ill-formed:

[edit] Parameters

other - another polymorphic object whose owned value (if exists) is used for assignment

[edit] Return value

*this

[edit] Exceptions

  1. If any exception is thrown, there are no effects on *this.

  2. If any exception is thrown, there are no effects on *this or other.

[edit] Example