swap(std::move_only_function)
From cppreference.com
< cpp | utility | functional | move only function
| friend void swap( std::move_only_function& lhs, std::move_only_function& rhs ) noexcept; |
(C++23から) | |
std::swap アルゴリズムを std::move_only_function にオーバーロードします。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)[編集] 関連項目
2つの std::move_only_function オブジェクトのターゲットをスワップする(public member function) | |
| (C++11) |
std::swap アルゴリズムを特殊化する (関数テンプレート) |