Namespaces
Variants

std::swap(std::any)

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

一般ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
swap および 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通ボキャブラリ型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
ヘッダ <any>
void swap( any& lhs, any& rhs ) noexcept;
(C++17以上)

std::swap アルゴリズムを std::any に対してオーバーロードします。2つの any オブジェクトの内容を、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 オブジェクトを交換する
(公開メンバ関数)