std::optional<T>::operator=
optional& operator=( std::nullopt_t ) noexcept;
|
(1) | (C++17부터) (C++20부터 constexpr) |
constexpr optional& operator=( const optional& other );
|
(2) | (C++17부터) |
constexpr optional& operator=( optional&& other ) noexcept(/* see below */);
|
(3) | (C++17부터) |
template< class U = std::remove_cv_t<T> >
optional& operator=( U&& value );
|
(4) | (C++17부터) (C++20부터 constexpr) |
template< class U >
optional& operator=( const optional<U>& other );
|
(5) | (C++17부터) (C++20부터 constexpr) |
template< class U >
optional& operator=( optional<U>&& other );
|
(6) | (C++17부터) (C++20부터 constexpr) |
*this의 내용을 other의 내용으로 대체합니다.
*this이 값을 포함하고 있다면, 포함된 값은 value().T::~T()을 통해 소멸자가 호출되어 소멸됩니다. 이 호출 후에는 *this이 값을 포함하지 않습니다.other의 상태를 할당합니다.
*this과other이 모두 값을 포함하지 않으면, 함수는 아무 효과가 없습니다.*this이 값을 포함하고 있지만other이 값을 포함하지 않으면, 포함된 값은 소멸자를 호출하여 소멸됩니다. 호출 후*this은 값을 포함하지 않습니다.other이 값을 포함하고 있으면,*this이 값을 포함하는지 여부에 따라 포함된 값이 직접 초기화되거나*other(2) 또는std::move(*other)(3)에서 할당됩니다. 이동된 optional은 여전히 값을 포함한다는 점에 유의하십시오.- 오버로드 (2)는
std::is_copy_constructible_v<T>또는std::is_copy_assignable_v<T>이false일 때 삭제됩니다.std::is_trivially_copy_constructible_v<T>,std::is_trivially_copy_assignable_v<T>및std::is_trivially_destructible_v<T>이 모두true이면 자명(trivial)합니다. - 오버로드 (3)은
std::is_move_constructible_v<T>또는std::is_move_assignable_v<T>이false일 때 오버로드 해결에 참여하지 않습니다.std::is_trivially_move_constructible_v<T>,std::is_trivially_move_assignable_v<T>및std::is_trivially_destructible_v<T>이 모두true이면 자명합니다.
*this이 값을 포함하는지 여부에 따라 포함된 값은 std::forward<U>(value)에서 직접 초기화되거나 std::forward<U>(value)에서 할당됩니다. 이 함수는 std::decay_t<U>(C++20까지)std::remove_cvref_t<U>(C++20부터)이 std::optional<T>이 아니고, std::is_constructible_v<T, U>이 true이고, std::is_assignable_v<T&, U>이 true이며, 다음 중 적어도 하나가 참일 때에만 오버로드 해결에 참여합니다:
T이 스칼라 타입이 아닙니다;std::decay_t<U>이T이 아닙니다.
other의 상태를 할당합니다.
*this과other이 모두 값을 포함하지 않으면, 함수는 아무 효과가 없습니다.*this이 값을 포함하고 있지만other이 값을 포함하지 않으면, 포함된 값은 소멸자를 호출하여 소멸됩니다. 호출 후*this은 값을 포함하지 않습니다.other이 값을 포함하고 있으면,*this이 값을 포함하는지 여부에 따라 포함된 값이 직접 초기화되거나*other(5) 또는std::move(*other)(6)에서 할당됩니다. 이동된 optional은 여전히 값을 포함한다는 점에 유의하십시오.- 다음 조건이 충족되는 경우에만 이러한 오버로드가 오버로드 해결에 참여합니다:
T이 (가능한const)std::optional<U>유형의 어떤 표현식으로부터도 생성 가능, 변환 가능 또는 할당 가능하지 않습니다. 즉, 다음 12개의 타입 특성(trait)이 모두false입니다:std::is_constructible_v<T, std::optional<U>&>std::is_constructible_v<T, const std::optional<U>&>std::is_constructible_v<T, std::optional<U>&&>std::is_constructible_v<T, const std::optional<U>&&>std::is_convertible_v<std::optional<U>&, T>std::is_convertible_v<const std::optional<U>&, T>std::is_convertible_v<std::optional<U>&&, T>std::is_convertible_v<const std::optional<U>&&, T>std::is_assignable_v<T&, std::optional<U>&>std::is_assignable_v<T&, const std::optional<U>&>std::is_assignable_v<T&, std::optional<U>&&>std::is_assignable_v<T&, const std::optional<U>&&>
- 오버로드 (5)의 경우,
std::is_constructible_v<T, const U&>과std::is_assignable_v<T&, const U&>이 모두true입니다. - 오버로드 (6)의 경우,
std::is_constructible_v<T, U>과std::is_assignable_v<T&, U>이 모두true입니다.
매개변수
| other | - |
포함된 값을 할당할 다른
optional
객체
|
| value | - | 포함된 값에 할당할 값 |
반환값
* this
예외
T
의 생성자나 할당 연산자에 의해 발생하는 모든 예외를 전파합니다. 예외가 발생할 경우,
*
this
(그리고
(
2-5
)
의 경우
other
)의 초기화 상태는 변경되지 않습니다. 즉, 객체가 값을 포함하고 있었다면 여전히 값을 포함하며, 그 반대의 경우도 마찬가지입니다.
value
의 내용과
*
this
및
other
의 포함된 값들은 예외가 발생한 연산(복사 생성자, 이동 할당 등)의 예외 안전성 보장에 따라 달라집니다.
std:: is_nothrow_move_constructible_v < T > )
참고 사항
선택적 객체
op
는 다음 두 가지 방법으로 빈 optional로 변환될 수 있습니다:
op
=
{
}
;
와
op
=
nullopt
;
. 첫 번째 표현식은
{
}
를 사용하여 빈
optional
객체를 생성하고 이를
op
에 할당합니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_optional
|
202106L
|
(C++20)
(DR20) |
완전한 constexpr ( 1 ) , ( 4-6 ) |
예제
#include <iostream> #include <optional> int main() { std::optional<const char*> s1 = "abc", s2; // constructor s2 = s1; // assignment s1 = "def"; // decaying assignment (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
출력:
abc def
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 3886 | C++17 |
오버로드
(
6
)
의 기본 템플릿 인수가
T
였음
|
std:: remove_cv_t < T > 로 변경됨 |
| P0602R4 | C++17 |
복사/이동 할당 연산자가 기본 연산이 trivial임에도
trivial하지 않을 수 있었음 |
triviality 전파 요구됨 |
| P2231R1 | C++20 | 오버로드 ( 1,4-6 ) 가 constexpr 가 아니었음 | constexpr 로 변경됨 |
참고 항목
|
포함된 값을 제자리에서 생성합니다
(public member function) |