Namespaces
Variants

std:: swap (std::any)

From cppreference.net
Utilities library
Definiert im Header <any>
void swap ( any & lhs, any & rhs ) noexcept ;
(seit C++17)

Überlädt den std::swap Algorithmus für std::any . Tauscht den Inhalt von zwei any Objekten durch Aufruf von lhs. swap ( rhs ) .

Parameter

lhs, rhs - zu vertauschende Objekte

Beispiel

#include <any>
#include <print>
#include <string>
int main()
{
    std::any p = 42, q = std::string{"Bishop"};
    std::println("p: {}, q: {}", std::any_cast<int>(p), std::any_cast<std::string&>(q));
    std::println("swap(p, q)");
    std::swap(p, q);
    std::println("p: {}, q: {}", std::any_cast<std::string&>(p), std::any_cast<int>(q));
}

Ausgabe:

p: 42, q: Bishop
swap(p, q)
p: Bishop, q: 42

Siehe auch

tauscht zwei any Objekte
(öffentliche Elementfunktion)