std::swap(std::function)
From 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 | - | 交換する多態関数ラッパー (polymorphic function wrappers) |
[編集] 戻り値
(なし)
[編集] 例
このコードを実行
#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++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2062 | C++11 | function のための swap のオーバーロードは noexcept である必要はありませんでした。 |
必要 |
[編集] 関連項目
| 内容を交換する (public member function) | |
| std::swap アルゴリズムを特殊化する (関数) |