Namespaces
Variants

swap (std::move_only_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* )
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 ) を呼び出します。

この関数は通常の unqualified lookup または qualified lookup では可視化されず、 std::move_only_function<FunctionType> が引数の関連クラスである場合にのみ argument-dependent lookup によって発見されます。

目次

パラメータ

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 オブジェクトのターゲットを交換する
(公開メンバ関数)
std::swap アルゴリズムを特殊化する
(関数テンプレート)