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* )
Definiert im Header <functional>
template < class R, class ... ArgTypes >

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

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

bool operator == ( std:: nullptr_t ,

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

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

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

bool operator ! = ( std:: nullptr_t ,

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

Vergleicht einen std::function mit einem Nullzeiger. Leere Funktionen (das heißt Funktionen ohne aufrufbares Ziel) vergleichen gleich, nicht-leere Funktionen vergleichen ungleich.

Der != -Operator wird synthetisiert aus operator== .

(seit C++20)

Inhaltsverzeichnis

Parameter

f - std::function zum Vergleichen

Rückgabewert

1,2) ! f
3,4) ( bool ) f

Beispiel

#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) // spezialisierter Vergleich mit 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);
}

Ausgabe:

7
8

Siehe auch

(C++23)
vergleicht ein std::move_only_function mit nullptr
(Funktion)