命名空间
变体
操作

std::swap(std::any)

来自 cppreference.cn
< cpp‎ | utility‎ | any
 
 
 
 
定义于头文件 <any>
void swap( any& lhs, any& rhs ) noexcept;
(自 C++17 起)

std::swap 算法重载了 std::any。通过调用 lhs.swap(rhs) 交换两个 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

[编辑] 参见

交换两个 any 对象
(公共成员函数) [编辑]