operator==(std::move_only_function)
来自cppreference.com
< cpp | utility | functional | move only function
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) // 与 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 (函数模板) |