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 또는 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)

참고 항목

두 개의 std::move_only_function 객체의 대상을 교환합니다
(public member function)
std::swap 알고리즘을 특수화합니다
(function template)