Namespaces
Variants

std:: swap (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 ... Args >
void swap ( std:: function < R ( Args... ) > & lhs, std:: function < R ( Args... ) > & rhs ) noexcept ;
(seit C++11)

Überlädt den std::swap -Algorithmus für std::function . Tauscht den Zustand von lhs mit dem von rhs aus. Ruft effektiv lhs. swap ( rhs ) auf.

Inhaltsverzeichnis

Parameter

lhs, rhs - polymorphe Funktionswrapper, deren Zustände ausgetauscht werden sollen

Rückgabewert

(keine)

Beispiel

#include <functional>
#include <iostream>
void foo(const char* str, int x)
{
    std::cout << "foo(\"" << str << "\", " << x << ")\n";
}
void bar(const char* str, int x)
{
    std::cout << "bar(\"" << str << "\", " << x << ")\n";
}
int main()
{
    std::function<void(const char*, int)> f1{foo};
    std::function<void(const char*, int)> f2{bar};
    f1("f1", 1);
    f2("f2", 2);
    std::cout << "std::swap(f1, f2);\n";
    std::swap(f1, f2);
    f1("f1", 1);
    f2("f2", 2);
}

Ausgabe:

foo("f1", 1)
bar("f2", 2)
std::swap(f1, f2);
bar("f1", 1)
foo("f2", 2)

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 2062 C++11 Überladung von swap für function war nicht als noexcept erforderlich erforderlich

Siehe auch

tauscht die Inhalte
(öffentliche Elementfunktion)
spezialisiert den std::swap Algorithmus
(Funktion)