命名空间
变体
动作

std::function<R(Args...)>::swap

来自 cppreference.cn
< cpp‎ | utility‎ | functional‎ | function
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
 
void swap( function& other ) noexcept;
(自 C++11 起)

交换 *thisother 的已存储可调用对象。

内容

[edit] 参数

other - 函数包装器,用于与其交换已存储的可调用对象

[edit] 返回值

(无)

[edit] 示例

#include <functional>
#include <iostream>
 
void foo(const char* str, int x)
{
    std::cout << "foo(\"" << str << "\", " << x << ")\n";
}
 
void bar(const char* str, int x)
{
    std::cout << "bar(\"" << str << "\", " << x << ")\n";
}
 
int main()
{
    std::function<void(const char*, int)> f1{foo};
    std::function<void(const char*, int)> f2{bar};
 
    f1("f1", 1);
    f2("f2", 2);
 
    std::cout << "f1.swap(f2);\n";
    f1.swap(f2);
 
    f1("f1", 1);
    f2("f2", 2);
}

输出

foo("f1", 1)
bar("f2", 2)
f1.swap(f2);
bar("f1", 1)
foo("f2", 2)

[edit] 参见

交换两个 std::move_only_function 对象的目标
(std::move_only_function 的公共成员函数) [edit]