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

来自cppreference.com
< cpp‎ | memory‎ | polymorphic
 
 
内存管理库
(仅用于阐述*)
分配器
未初始化内存算法
受约束的未初始化内存算法
内存资源
未初始化存储 (C++20 前)
(C++17 弃用)
(C++17 弃用)

垃圾收集器支持 (C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
(C++11)(C++23 前)
 
 
constexpr polymorphic& operator=( const polymorphic& other );
(1) (C++26 起)
constexpr polymorphic& operator=( polymorphic&& other )
    noexcept(/* 见下文 */);
(2) (C++26 起)

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

traitsstd::allocator_traits<Allocator>

1) 如果 std::addressof(other) == thistrue,那么什么也不做。否则,设 need_updatetraits::propagate_on_container_copy_assignment::value
  1. 如果 other 无值,那么直接前往下一步。否则通过 traits::construct 以分配器 update_alloc ? other.alloc : alloc*this 中以 *other 作为实参构造新的拥有的对象。
  2. 通过 traits::destroy 销毁 *this 中先前拥有的对象(如果存在),然后解分配该对象使用的存储。
更新 *this 拥有的对象后,如果 need_updatetrue,那么就会以 other.alloc 的副本替换 alloc
如果 T不完整类型,那么程序非良构。
2) 如果 std::addressof(other) == thistrue,那么什么也不做。否则,设 need_updatetraits::propagate_on_container_move_assignment::value
  • 如果 alloc == other.alloctrue,那么交换 *thisother 拥有的对象;通过 traits::destroy 销毁 other 中拥有的对象(如果存在),然后解分配该对象使用的存储。
  • 否则:
  1. 如果 other 无值,那么直接前往下一步。否则通过 traits::construct 以分配器 update_alloc ? other.alloc : alloc*this 中以 std::move(*other) 作为实参构造新的拥有的对象。
  2. 通过 traits::destroy 销毁 *this 中先前拥有的对象(如果存在),然后解分配该对象使用的存储。
更新 *this 拥有的对象后,如果 need_updatetrue,那么就会以 other.alloc 的副本替换 alloc
如果满足以下所有条件,那么程序非良构:

参数

other - 拥有的值(如果存在)用于赋值的另一 polymorphic 对象

返回值

*this

异常

1) 如果抛出了异常,那么 *this 不会受到影响。
2) 如果抛出了异常,那么 *thisother 不会受到影响。
noexcept 说明:  
noexcept(std::allocator_traits<Allocator>::

             propagate_on_container_move_assignment::value

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

示例