C++ 参考手册

void swap( optional& other ) noexcept(/* see below */);
(C++17 èµ·)

与 other 交换内容。

  • è‹¥ *this å’Œ other 均不含值,则函数无效果。
  • è‹¥ *this 与 other 仅有一个含值(称此对象为 in ,另一者为 un ),则从 std::move(*in) 直接初始化 un 所含值,随后如同通过 in->T::~T() 析构 in 所含值。此调用后, in 不含值; un 含值。
  • è‹¥ *this 与 other 均含值,则通过调用 std::swap(**this, *other) 交换所含值。 T 左值必须满足可交换 (Swappable) 。

参数

other - 要交换内容的 optional 对象

返回值

(无)

异常

noexcept 规定:  
noexcept(std::is_nothrow_move_constructible_v<T> &&
         std::is_nothrow_swappable_v<T>)

在抛异常的情况下, *this 和 other 所含值的状态由 T 的 swap 或 T 的移动构造函数的异常安全保证确定,取决于所调用者。对于 *this 和 other ,若对象含值,则令它继续含值,反之亦然。

参阅

特化 std::swap 算法
(函数)