operator==(std::move_only_function)
来自 cppreference.cn
< cpp | utility | functional | move only function
friend bool operator==( const std::move_only_function& f, std::nullptr_t ) noexcept; |
(since C++23) | |
检查包装器 f 是否具有可调用目标,通过形式上将其与 std::nullptr_t 进行比较。空包装器(即,没有目标的包装器)比较相等,非空函数比较不相等。
此函数对于普通的非限定或限定查找是不可见的,并且只能通过实参依赖查找找到,当 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::function 与 nullptr 进行比较 (函数模板) |