operator==,!=(std::function)
來自 cppreference.com
< cpp | utility | functional | function
定義於標頭檔案 <functional> |
||
template< class R, class... ArgTypes > bool operator==( const std::function<R(ArgTypes...)>& f, |
(1) | (C++11 起) |
template< class R, class... ArgTypes > bool operator==( std::nullptr_t, |
(2) | (C++11 起) (C++20 前) |
template< class R, class... ArgTypes > bool operator!=( const std::function<R(ArgTypes...)>& f, |
(3) | (C++11 起) (C++20 前) |
template< class R, class... ArgTypes > bool operator!=( std::nullptr_t, |
(4) | (C++11 起) (C++20 前) |
將 `std::function` 與空指標進行比較。空函式(即沒有可呼叫目標的函式)比較結果為相等,非空函式比較結果為不相等。
|
(C++20 起) |
目錄 |
[編輯] 引數
f | - | 要比較的 `std::function` |
[編輯] 返回值
1,2) !f
3,4) (bool) f
[編輯] 示例
執行此程式碼
#include <functional> #include <iostream> using SomeVoidFunc = std::function<void(int)>; class C { public: C(SomeVoidFunc void_func = nullptr) : void_func_(void_func) { if (void_func_ == nullptr) // specialized compare with nullptr void_func_ = std::bind(&C::default_func, this, std::placeholders::_1); void_func_(7); } void default_func(int i) { std::cout << i << '\n'; }; private: SomeVoidFunc void_func_; }; void user_func(int i) { std::cout << (i + 1) << '\n'; } int main() { C c1; C c2(user_func); }
輸出
7 8
[編輯] 參閱
(C++23) |
將 std::move_only_function 與 nullptr 進行比較(function) |