名前空間
変種
操作

std::swap(std::any)

From cppreference.com
< cpp‎ | utility‎ | any
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダー <any> で定義
void swap( any& lhs, any& rhs ) noexcept;
(C++17以降)

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

[編集] パラメータ

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 オブジェクトを交換する
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)