Namespaces
Variants

std::polymorphic<T, Allocator>:: operator=

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
constexpr polymorphic & operator = ( const polymorphic & other ) ;
(1) (自 C++26 起)
constexpr polymorphic & operator = ( polymorphic && other )
noexcept ( /* 见下文 */ ) ;
(2) (自 C++26 起)

* this 的内容替换为 other 的内容。

traits std:: allocator_traits < Allocator >

1) std:: addressof ( other ) == this true ,则不执行任何操作。否则,令 need_update traits :: propagate_on_container_copy_assignment :: value
  1. other 为空值,则继续下一步。否则,使用分配器 update_alloc ? other. alloc : alloc ,以 * other 为参数,通过 traits :: construct * this 中构造新的持有对象。
  2. * this 中先前持有的对象(若存在)通过 traits :: destroy 销毁,随后释放其存储空间。
在更新由 * this 拥有的对象后,若 need_update true ,则 alloc 将被替换为 other. alloc 的副本。
如果 T 不完整类型 ,则程序非良构。
2) std:: addressof ( other ) == this true ,则不执行任何操作。否则,令 need_update traits :: propagate_on_container_move_assignment :: value
  • alloc == other. alloc true ,则交换 * this other 所拥有的对象;随后使用 traits :: destroy 销毁 other 中的对象(若存在)并释放存储空间。
  • 否则:
  1. other 为空值,则直接进入下一步。否则,使用 traits :: construct * this 中构造新对象,以 std :: move ( * other ) 为参数,并使用分配器 update_alloc ? other. alloc : alloc
  2. 使用 traits :: destroy 销毁 * this 中原有对象(若存在)并释放存储空间。
在更新由 * this other 拥有的对象后,若 need_update true ,则 alloc 将被替换为 other. alloc 的副本。
若满足以下所有条件,则程序非良构:

目录

参数

其他 - 另一个 polymorphic 对象,其拥有的值(如果存在)将用于赋值

返回值

* this

异常

1) 如果抛出任何异常,对 * this 不会产生任何影响。
2) 若抛出任何异常,对 * this other 均不产生任何影响。
noexcept 规范:
noexcept ( std:: allocator_traits < Allocator > ::

propagate_on_container_move_assignment :: value

|| std:: allocator_traits < Allocator > :: is_always_equal :: value )

示例