名稱空間
變體
操作

std::swap(std::function)

來自 cppreference.com
< 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*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(直到 C++20*)
(直到 C++20*)
 
 
定義於標頭檔案 <functional>
template< class R, class... Args >
void swap( std::function<R(Args...)>& lhs, std::function<R(Args...)>& rhs ) noexcept;
(C++11 起)

過載 std::swap 演算法,用於 std::function。交換 lhsrhs 的狀態。有效地呼叫 lhs.swap(rhs)

目錄

[編輯] 引數

lhs, rhs - 要交換狀態的多型函式包裝器

[編輯] 返回值

(無)

[編輯] 示例

#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 << "std::swap(f1, f2);\n";
    std::swap(f1, f2);
 
    f1("f1", 1);
    f2("f2", 2);
}

輸出

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

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 2062 C++11 functionswap 過載不要求是 noexcept 需要

[編輯] 參閱

交換內容
(public member function) [編輯]
特化 std::swap 演算法
(function) [編輯]