std::swap(std::function)
ja.cppreference.net より
| ヘッダ <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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2062 | C++11 |
function
に対する
swap
のオーバーロードはnoexceptであることが要求されていなかった
|
要求される |
関連項目
|
内容を交換する
(公開メンバ関数) |
|
|
(C++23)
|
std::swap
アルゴリズムを特殊化する
(関数) |