std:: swap (std::function)
From cppreference.net
<
cpp
|
utility
|
functional
|
function
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::function
| Member functions | ||||
|
(until C++17)
|
||||
| Non-member functions | ||||
|
(until C++20)
|
||||
|
swap
(std::function)
|
||||
| Helper classes | ||||
|
(until C++17)
|
||||
| Deduction guides (C++17) |
|
ヘッダーで定義
<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
アルゴリズムを特殊化する
(関数) |