Namespaces
Variants

std::move_only_function::operator=

来自 zh.cppreference.net
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧的绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 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,或若 *this 为空则通过 other 销毁 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...)> 的公开成员函数)