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가 요구되지 않았음 요구됨

참고 항목

내용을 교환합니다
(public member function)
std::swap 알고리즘을 특수화합니다
(function)