Namespaces
Variants

std::move_only_function:: operator()

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* )
R operator ( ) ( Args... args ) /*cv*/ /*ref*/ noexcept ( /*noex*/ ) ;
(seit C++23)

Ruft das gespeicherte aufrufbare Ziel mit den Parametern args auf. Die /*cv*/ -, /*ref*/ - und /*noex*/ -Teile von operator ( ) sind identisch mit denen des Template-Parameters von std::move_only_function .

Entspricht return std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) ; , wobei f ein cv-unqualifizierter Lvalue ist, der das Zielobjekt von * this bezeichnet, und /*cv-ref-cast*/ ( f ) äquivalent ist zu:

  • f falls cv ref entweder leer oder & ist, oder
  • std:: as_const ( f ) falls cv ref entweder const oder const & ist, oder
  • std :: move ( f ) falls cv ref && ist, oder
  • std :: move ( std:: as_const ( f ) ) falls cv ref const && ist.

Das Verhalten ist undefiniert, wenn * this leer ist.

Inhaltsverzeichnis

Parameter

args - Parameter, die an das gespeicherte aufrufbare Ziel übergeben werden

Rückgabewert

std:: invoke_r < R > ( /*cv-ref-cast*/ ( f ) , std:: forward < Args > ( args ) ... ) .

Ausnahmen

Verbreitet die Ausnahme, die durch den zugrunde liegenden Funktionsaufruf ausgelöst wird.

Beispiel

Das folgende Beispiel zeigt, wie std::move_only_function an andere Funktionen als Wert übergeben werden kann. Außerdem zeigt es, wie std::move_only_function Lambdas speichern kann.

#include <iostream>
#include <functional>
void call(std::move_only_function<int() const> f)  // can be passed by value
{ 
    std::cout << f() << '\n';
}
int normal_function() 
{
    return 42;
}
int main()
{
    int n = 1;
    auto lambda = [&n](){ return n; };
    std::move_only_function<int() const> f = lambda;
    call(std::move(f));
    n = 2;
    call(lambda); 
    f = normal_function; 
    call(std::move(f));
}

Ausgabe:

1
2
42

Siehe auch

ruft das Zielobjekt auf
(öffentliche Elementfunktion von std::function<R(Args...)> )
ruft die gespeicherte Funktion auf
(öffentliche Elementfunktion von std::reference_wrapper<T> )
(C++17) (C++23)
ruft jedes Callable Objekt mit gegebenen Argumenten auf mit Möglichkeit zur Rückgabetypspezifikation (seit C++23)
(Funktionstemplate)