operator==(std::move_only_function)
来自 zh.cppreference.net
friend bool operator==( const std::move_only_function& f, std::nullptr_t ) noexcept;
|
(自 C++23 起) | |
检查包装器 f 是否包含可调用目标,通过将其与 std::nullptr_t 进行形式比较。空包装器(即没有目标的包装器)比较结果为相等,非空函数比较结果为不相等。
此函数不可见于普通的非限定查找或限定查找,只能在参数为关联类时通过std::move_only_function<FunctionType>实参依赖查找
找到。
!= operator!= 运算符从 operator==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
进行比较
(函数模板) |