Namespaces
Variants

operator== (std::move_only_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* )
friend bool operator == ( const std:: move_only_function & f, std:: nullptr_t ) noexcept ;
(seit C++23)

Prüft, ob der Wrapper f ein aufrufbares Ziel besitzt, indem er formal mit std::nullptr_t verglichen wird. Leere Wrapper (also Wrapper ohne Ziel) vergleichen gleich, nicht-leere Funktionen vergleichen ungleich.

Diese Funktion ist für gewöhnliches unqualified oder qualified lookup nicht sichtbar und kann nur durch argument-dependent lookup gefunden werden, wenn std::move_only_function<FunctionType> eine assoziierte Klasse der Argumente ist.

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

Inhaltsverzeichnis

Parameter

f - std::move_only_function zu vergleichen

Rückgabewert

! f .

Beispiel

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

Ausgabe:

7
8

Siehe auch

prüft, ob das std::move_only_function ein Zielobjekt besitzt
(öffentliche Elementfunktion)
(entfernt in C++20)
vergleicht ein std::function mit nullptr
(Funktionstemplate)