命名空间
变体
操作

operator==(std::move_only_function)

来自 cppreference.cn
 
 
 
函数对象
函数调用
(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*)
 
 
friend bool operator==( const std::move_only_function& f, std::nullptr_t ) noexcept;
(C++23 起)

通过形式上与 std::nullptr_t 比较,检查包装器 f 是否具有可调用目标。空包装器(即没有目标的包装器)比较相等,非空函数比较不相等。

此函数对于普通的非限定限定查找不可见,只能通过实参依赖查找找到,当 std::move_only_function<FunctionType> 是实参的关联类时。

!= 运算符由 operator== 合成

目录

[编辑] 参数

f - 要比较的 std::move_only_function

[编辑] 返回值

!f.

[编辑] 示例

#include <functional>
#include <iostream>
#include <utility>
 
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) // specialized compare with 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

[编辑] 参阅

检查 std::move_only_function 是否有目标
(公共成员函数) [编辑]
(在 C++20 中移除)
比较 std::functionnullptr
(函数模板) [编辑]