std::function<R(Args...)>::swap
來自 cppreference.com
< cpp | utility | functional | function
void swap( function& other ) noexcept; |
(C++11 起) | |
交換 *this 與 other
所儲存的可呼叫物件。
目錄 |
[編輯] 引數
其他 | - | 用於交換所儲存的可呼叫物件的函式包裝器 |
[編輯] 返回值
(無)
[編輯] 示例
執行此程式碼
#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)
[編輯] 參閱
交換兩個 std::move_only_function 目標( std::move_only_function 的公開成員函式) |