operator==(std::move_only_function)
來自 cppreference.com
< cpp | 工具庫 | 函式物件 | 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) // 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 (函式模板) |