swap(std::move_only_function)
來自 cppreference.com
< cpp | 工具庫 | 函式物件 | move only function
friend void swap( std::move_only_function& lhs, std::move_only_function& rhs ) noexcept; |
(C++23 起) | |
為 std::move_only_function 過載 std::swap 演算法。交換 lhs 與 rhs 的狀態。實質上呼叫 lhs.swap(rhs)。
此函式對於一般的非限定查詢或限定查詢不可見,僅當 std::move_only_function<FunctionType>
是引數的關聯類時,才能透過實參依賴查詢找到它。
目錄 |
[編輯] 引數
lhs, rhs | - | 要交換狀態的 std::move_only_function 物件 |
[編輯] 返回值
(無)
[編輯] 示例
執行此程式碼
#include <concepts> #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::move_only_function<void(const char*, int) const> f1{foo}; std::move_only_function<void(const char*, int) const> f2{bar}; f1("f1", 1); f2("f2", 2); std::cout << "std::ranges::swap(f1, f2);\n"; std::ranges::swap(f1, f2); // finds the hidden friend f1("f1", 1); f2("f2", 2); }
輸出
foo("f1", 1) bar("f2", 2) std::ranges::swap(f1, f2); bar("f1", 1) foo("f2", 2)
[編輯] 參閱
交換兩個 std::move_only_function 目標(public member function) | |
(C++11) |
特化 std::swap 演算法 (function template) |