std::swap(std::function)
來自 cppreference.com
< cpp | utility | functional | function
定義於標頭檔案 <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。交換 lhs 和 rhs 的狀態。有效地呼叫 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 | function 的 swap 過載不要求是 noexcept |
需要 |
[編輯] 參閱
交換內容 (public member function) | |
特化 std::swap 演算法 (function) |