operator==(std::move_only_function)

friend bool operator==( const move_only_function& f, std::nullptr_t ) noexcept;
(C++23 起)

std::move_only_functionstd::nullptr_t 比较。空包装(即无可调用目标的包装)比较相等,非空包装比较不相等。

此函数对通常无限定有限定查找不可见,而只能在 std::move_only_function<FunctionType> 为参数的关联类时由实参依赖查找找到。

!= 运算符从 operator== 合成

参数

f - 要比较的 std::move_only_function

返回值

!f

示例

#include <functional>
#include <iostream>
 
using SomeVoidFunc = std::move_only_function<void(int) const>;
 
class C {
public:
    C() = default;
    C(SomeVoidFunc func) : void_func_(std::move(func)) {}
 
    void default_func(int i) const { std::cout << i << '\n'; };
 
    void operator()() const
    {
        if (void_func_ == nullptr) // 与 nullptr 的特殊比较
            default_func(7);
        else
            void_func_(7);
    }
 
private:
    SomeVoidFunc void_func_{};
};
 
void user_func(int i)
{
    std::cout << (i + 1) << '\n';
}
 
int main()
{
    C c1;
    C c2(user_func);
    c1();
    c2();
}

输出:

7
8

参阅

(C++20 中移除)
比较 std::functionnullptr
(函数模板)