operator==,!=(std::function)
From 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 を比較する(関数) |