Namespaces
Variants

std:: swap (std::any)

From cppreference.net
Utilities library
ヘッダーで定義 <any>
void swap ( any & lhs, any & rhs ) noexcept ;
(C++17以降)

std::swap アルゴリズムを std::any に対してオーバーロードします。 any オブジェクト2つの内容を lhs. swap ( rhs ) を呼び出すことで交換します。

パラメータ

lhs, rhs - 交換するオブジェクト

#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));
}

出力:

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

関連項目

2つの any オブジェクトを交換する
(公開メンバ関数)