operator== (std::move_only_function)
From cppreference.net
<
cpp
|
utility
|
functional
|
move only function
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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==
自动合成。
目录 |
参数
| 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
进行比较
(函数模板) |