Namespaces
Variants

std::move_only_function:: operator=

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
move_only_function & operator = ( move_only_function && other ) ;
(1) (自 C++23 起)
move_only_function & operator = ( const move_only_function & ) = delete ;
(2) (自 C++23 起)
move_only_function & operator = ( std:: nullptr_t ) noexcept ;
(3) (自 C++23 起)
template < class F >
move_only_function & operator = ( F && f ) ;
(4) (自 C++23 起)

std::move_only_function 分配新目标或销毁其现有目标。

1) other 的目标移动至 * this ,或当 other 为空时销毁 * this 的目标(如果存在),通过 auto ( std :: move ( other ) ) . swap ( * this ) 实现。移动赋值后, other 处于有效状态但具有未指定的值。
2) 复制赋值运算符被删除。 std::move_only_function 不满足 CopyAssignable 要求。
3) 若当前目标存在则销毁之。调用后 * this 为空状态。
4) * this 的目标设置为可调用对象 f ,若 f 是空函数指针、空成员函数指针或空的 std::move_only_function ,则销毁当前目标,操作等效于执行 move_only_function ( std:: forward < F > ( f ) ) . swap ( * this ) ; 。此重载仅当 move_only_function 的构造函数能通过重载决议从 F 构造时才参与重载决议。若选定的构造函数调用存在格式错误或未定义行为,则程序格式错误或具有未定义行为。

目录

参数

other - 用于移动目标的另一个 std::move_only_function 对象
f - 用于初始化新目标的可调用对象

返回值

* this

注释

特意不要求移动赋值运算符为 noexcept ,以便为将来实现支持分配器的 move_only_function 留出空间。

move_only_function 可以通过 std:: in_place_type < Fn > 进行赋值,前提是它可以从该参数构造。

示例

参见

分配新的目标对象
( std::function<R(Args...)> 的公开成员函数)