Namespaces
Variants

std:: swap (std::function)

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
ヘッダーで定義 <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であることが要求されていなかった 要求される

関連項目

内容を交換する
(公開メンバ関数)
std::swap アルゴリズムを特殊化する
(関数)