std::function<R(Args...)>:: operator=
From cppreference.net
<
cpp
|
utility
|
functional
|
function
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
std::function
| Member functions | ||||
|
function::operator=
|
||||
|
(until C++17)
|
||||
| Non-member functions | ||||
|
(until C++20)
|
||||
| Helper classes | ||||
|
(until C++17)
|
||||
| Deduction guides (C++17) |
|
function
&
operator
=
(
const
function
&
other
)
;
|
(1) | (C++11부터) |
|
function
&
operator
=
(
function
&&
other
)
;
|
(2) | (C++11부터) |
|
function
&
operator
=
(
std::
nullptr_t
)
noexcept
;
|
(3) | (C++11부터) |
|
template
<
class
F
>
function & operator = ( F && f ) ; |
(4) | (C++11부터) |
|
template
<
class
F
>
function & operator = ( std:: reference_wrapper < F > f ) noexcept ; |
(5) | (C++11부터) |
std::function
에 새로운
대상
을 할당합니다.
1)
other
의
target
복사본을 할당합니다. 마치
function
(
other
)
.
swap
(
*
this
)
;
를 실행하는 것처럼 동작합니다.
2)
other
의
대상
을
*
this
로 이동시킵니다.
other
는 유효한 상태이지만 지정되지 않은 값을 가집니다.
3)
현재
target
을 해제합니다.
*
this
는 호출 후
empty
상태가 됩니다.
4)
target
을 호출 가능한
f
로 설정합니다. 마치
function
(
std::
forward
<
F
>
(
f
)
)
.
swap
(
*
this
)
;
를 실행하는 것과 같습니다. 이 연산자는
f
가 인수 타입
Args...
와 반환 타입
R
에 대해
Callable
인 경우에만 오버로드 해결에 참여합니다.
5)
target
을
*
this
의 복사본으로 설정합니다. 마치
function
(
f
)
.
swap
(
*
this
)
;
을 실행하는 것처럼 동작합니다.
목차 |
매개변수
| other | - |
대상(target)을 복사할 또 다른
std::function
객체
|
| f | - | target 을 초기화하는 데 사용할 호출 가능 객체 |
| 타입 요구사항 | ||
-
F
는
Callable
요구사항을 충족해야 합니다.
|
||
반환값
* this
참고 사항
C++17에서
std::function
의 할당자 지원이 제거되기 전부터, 이러한 대입 연산자들은
*
this
또는
other
의 할당자가 아닌 기본 할당자를 사용합니다(
LWG 이슈 2386
참조).
예제
이 코드 실행
#include <cassert> #include <functional> #include <utility> int inc(int n) { return n + 1; } int main() { std::function<int(int)> f1; std::function<int(int)> f2(inc); assert(f1 == nullptr and f2 != nullptr); f1 = f2; // 오버로드 (1) assert(f1 != nullptr and f1(1) == 2); f1 = std::move(f2); // 오버로드 (2) assert(f1 != nullptr and f1(1) == 2); // f2는 유효하지만 지정되지 않은 상태임 f1 = nullptr; // 오버로드 (3) assert(f1 == nullptr); f1 = inc; // 오버로드 (4) assert(f1 != nullptr and f1(1) == 2); f1 = [](int n) { return n + n; }; // 오버로드 (4) assert(f1 != nullptr and f1(2) == 4); std::reference_wrapper<int(int)> ref1 = std::ref(inc); f1 = ref1; // 오버로드 (5) assert(f1 != nullptr and f1(1) == 2); }
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2132 | C++11 | ( 4 ) Callable 객체를 취하는 오버로드가 모호할 수 있음 | 제약됨 |
| LWG 2401 | C++11 |
(
3
)
std::nullptr_t
에서의 할당 연산자가 noexcept로 요구되지 않음
|
요구됨 |
참고 항목
|
대상을 교체하거나 파괴함
(
std::move_only_function
의
public member function)
|
|
|
(C++17에서 제거됨)
|
새로운 대상을 할당함
(public member function) |