Namespaces
Variants

std::indirect<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 indirect & operator = ( const indirect & other ) ;
(1) (自 C++26 起)
constexpr indirect & operator = ( indirect && other ) noexcept ( /* 见下文 */ ) ;
(2) (自 C++26 起)
template < class U = T >
constexpr indirect & operator = ( U && value ) ;
(3) (自 C++26 起)

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

traits std:: allocator_traits < Allocator >

1) std:: addressof ( other ) == this true ,则不执行任何操作。否则,令 need_update traits :: propagate_on_container_copy_assignment :: value
  • other 为空值状态,则 * this 变为空值状态,且 * this 所拥有的对象(若存在)将通过 traits :: destroy 销毁,随后释放存储空间。
  • 否则,若 alloc == other. alloc true * this 非空值状态,则等价于 ** this = * other
  • 否则:
  1. 使用 traits :: construct * this 中构造新的拥有对象,以 * other 为参数,并使用分配器 update_alloc ? other. alloc : alloc
  2. * this 中原先拥有的对象(若存在)将通过 traits :: destroy 销毁,随后释放存储空间。
  3. p 指向新拥有的对象。
在更新由 * this 拥有的对象后,若 need_update true ,则 alloc 将被替换为 other. alloc 的副本。
std:: is_copy_assignable_v < T > && std:: is_copy_constructible_v < T > false ,则程序非良构。
2) std:: addressof ( other ) == this true ,则不执行任何操作。否则,令 need_update traits :: propagate_on_container_move_assignment :: value
  • other 为空值状态,则 * this 变为空值状态,且 * this 所拥有的对象(若存在)通过 traits :: destroy 销毁,随后释放存储空间。
  • 否则,若 alloc == other. alloc true ,则交换 * this other 所拥有的对象;随后 other 中的对象(若存在)通过 traits :: destroy 销毁,并释放存储空间。
  • 否则:
  1. 使用 traits :: construct * this 中构造新对象,以 std :: move ( * other ) 为参数,并使用分配器 update_alloc ? other. alloc : alloc
  2. * this 中原有对象(若存在)通过 traits :: destroy 销毁,随后释放存储空间。
  3. p 指向新构造的对象。
在更新由 * this other 拥有的对象后,若 need_update true ,则 alloc 将被替换为 other. alloc 的副本。
std:: is_copy_constructible_v < T > false ,则程序非良构。
3) * this 为无值状态,则使用 alloc 通过 std:: forward < U > ( value ) 构造持有对象。否则,等价于执行 ** this = std:: forward < U > ( value )
此重载仅在满足以下所有条件时参与重载决议:

目录

参数

other - 另一个 indirect 对象,其拥有的值(若存在)将用于赋值
value - 用于赋值或构造所拥有值的值

返回值

* this

异常

1) 若抛出任何异常, this - > valueless_after_move ( ) 的结果保持不变。
如果在调用 T 的选定复制构造函数期间抛出异常,则无任何效果。
如果在调用 T 的复制赋值运算符期间抛出异常,则 this - > p 的状态由 T 的复制赋值运算符的异常安全保证定义。
2) 若抛出任何异常,对 * this other 均不产生任何影响。
noexcept 规范:
noexcept ( std:: allocator_traits < Allocator > ::

propagate_on_container_move_assignment :: value

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

示例