Namespaces
Variants

operator==,!= (std::function)

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
ヘッダーで定義 <functional>
template < class R, class ... ArgTypes >

bool operator == ( const std:: function < R ( ArgTypes... ) > & f,

std:: nullptr_t ) noexcept ;
(1) (C++11以降)
template < class R, class ... ArgTypes >

bool operator == ( std:: nullptr_t ,

const std:: function < R ( ArgTypes... ) > & f ) noexcept ;
(2) (C++11以降)
(C++20以前)
template < class R, class ... ArgTypes >

bool operator ! = ( const std:: function < R ( ArgTypes... ) > & f,

std:: nullptr_t ) noexcept ;
(3) (C++11以降)
(C++20以前)
template < class R, class ... ArgTypes >

bool operator ! = ( std:: nullptr_t ,

const std:: function < R ( ArgTypes... ) > & f ) noexcept ;
(4) (C++11以降)
(C++20以前)

std::function をヌルポインタと比較します。空の関数(つまり、呼び出し可能なターゲットを持たない関数)は等しいと比較され、空でない関数は等しくないと比較されます。

!= 演算子は operator== から合成されます

(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) // 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 を比較する
(関数)