std::expected<T, E>::operator=
constexpr expected& operator=( const expected& other );
|
(1) | (C++23부터) |
constexpr expected& operator=( expected&& other ) noexcept(/*see below*/);
|
(2) | (C++23부터) |
template< class U = std::remove_cv_t<T> >
constexpr expected& operator=( U&& v );
|
(3) | (C++23부터) (T가 cv void가 아님) |
template< class G >
constexpr expected& operator=( const unexpected<G>& other );
|
(4) | (C++23부터) |
template< class G >
constexpr expected& operator=( unexpected<G>&& other );
|
(5) | (C++23부터) |
기존 expected 객체에 새 값을 할당합니다.
other의 상태를 할당합니다.
- 만약
this->has_value()이other.has_value()과 같으면,other에 포함된 값을 할당합니다.T이 (아마도 cv-한정된)void이고other.has_value()가true이면 아무것도 하지 않습니다. - 그렇지 않으면, 현재 포함된 값을 파괴하고 (
this->has_value()이true이고T가 (아마도 cv-한정된)void이면 아무것도 하지 않음),*this가other에 포함된 값의 복사본을 포함하도록 만듭니다.
- 만약
other.has_value()이true이고T이 (아마도 cv-한정된)void이면, 새 값을 생성하지 않습니다. 그렇지 않으면, 새 값은 적절하게 (1) (2)*other또는other.error()로부터 복사 생성 또는 이동 생성됩니다. 예외가 발생하면, 이전 값이 유지됩니다;*this는 값 없음 상태가 되지 않습니다.
예외가 발생하지 않았다면, 할당 후 has_value()는 other.has_value()과 같습니다.
- 오버로드 (1)는 다음 조건이 아닌 한 삭제된 것으로 정의됩니다:
T이 (아마도 cv-한정된)void이거나std::is_copy_assignable_v<T>가true이고,T이 (아마도 cv-한정된)void이거나std::is_copy_constructible_v<T>가true이고,std::is_copy_assignable_v<E>가true이고,std::is_copy_constructible_v<E>이true이고,- 다음 중 적어도 하나가
true인 경우:T이 (아마도 cv-한정된)voidstd::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
- 오버로드 (2)는 다음 조건이 충족될 때만 오버로드 해결에 참여합니다:
T이 (아마도 cv-한정된)void이거나std::is_move_assignable_v<T>가true이고,T이 (아마도 cv-한정된)void이거나std::is_move_constructible_v<T>가true이고,std::is_move_assignable_v<E>가true이고,std::is_move_constructible_v<E>이true이고,- 다음 중 적어도 하나가
true인 경우:T이 (아마도 cv-한정된)voidstd::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
- 만약
this->has_value()이true이면,**this = std::forward<U>(v)과 동등합니다. - 그렇지 않으면,
*this에 포함된 값을 파괴하고,*this가std::forward<U>(v)로부터 초기화된 값을 포함하도록 만듭니다. 예외가 발생하면, 이전 값이 유지됩니다;*this는 값 없음 상태가 되지 않습니다.
예외가 발생하지 않았다면, 할당 후 this->has_value()은 true입니다.
- 이 오버로드는 다음 조건이 충족될 때만 오버로드 해결에 참여합니다:
std::is_same_v<expected, std::remove_cvref_t<U>>이false이고,std::remove_cvref_t<U>가std::unexpected의 특수화가 아니고,std::is_constructible_v<T, U>이true이고,std::is_assignable_v<T&, U>가true이고,- 다음 중 적어도 하나가
true인 경우:std::is_nothrow_constructible_v<T, U>std::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
GF을 오버로드 const G&(4)에 대해서는 , 오버로드 G(5)에 대해서는 라고 합시다.
- 만약
this->has_value()이true이면,*this에 포함된 값을 파괴하고 (T이 (아마도 cv-한정된)void이면 아무것도 하지 않음),*this가std::forward<GF>(e.error())로부터 초기화된 값을 포함하도록 만듭니다. 예외가 발생하면, 이전 값이 유지됩니다;*this는 값 없음 상태가 되지 않습니다. - 그렇지 않으면,
this->error() = std::forward<GF>(e.error())과 동등합니다.
예외가 발생하지 않았다면, 할당 후 this->has_value()는 false입니다.
- 이 오버로드는 다음 조건이 충족될 때만 오버로드 해결에 참여합니다:
std::is_constructible_v<E, GF>이true이고,std::is_assignable_v<E&, GF>가true이고,- 다음 중 적어도 하나가
true인 경우:T이 (아마도 cv-한정된)voidstd::is_nothrow_constructible_v<E, GF>std::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
모든 경우에, T이 (아마도 cv-한정된) void이 아니라면, 이전 값의 파괴와 새 값의 생성은 다음 설명 전용 함수 reinit_expected에 의해 수행됩니다.
template<class NewType, class OldType, class... Args>
constexpr void reinit_expected(NewType& new_val, OldType& old_val, Args&&... args)
{
if constexpr (std::is_nothrow_constructible_v<NewType, Args...>)
{
std::destroy_at(std::addressof(old_val));
std::construct_at(std::addressof(new_val), std::forward<Args>(args)...);
}
else if constexpr (std::is_nothrow_move_constructible_v<NewType>)
{
NewType temp(std::forward<Args>(args)...); // may throw
std::destroy_at(std::addressof(old_val));
std::construct_at(std::addressof(new_val), std::move(temp));
}
else
{
OldType temp(std::move(old_val));
std::destroy_at(std::addressof(old_val));
try
{
std::construct_at(std::addressof(new_val), std::forward<Args>(args)...); // may throw
}
catch (...)
{
std::construct_at(std::addressof(old_val), std::move(temp));
throw;
}
}
}
매개변수
| other | - |
할당할 포함된 값을 가진 다른
expected
객체
|
| v | - | 포함된 값에 할당할 값 |
| e | - | std::unexpected 객체의 포함된 값을 할당 |
| newval | - | 생성될 포함된 값 |
| oldval | - | 소멸될 포함된 값 |
| args | - | newval 의 초기화자로 사용되는 인수들 |
반환값
예외
std::
is_nothrow_move_constructible_v
<
T
>
&&
std::
is_nothrow_move_assignable_v
<
T
>
&&
예제
|
이 섹션은 불완전합니다
이유: 예제 없음 |
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 수정된 동작 |
|---|---|---|---|
| LWG 3886 | C++23 |
오버로드
(
3
)
의 기본 템플릿 인수가
T
였음
|
std:: remove_cv_t < T > 로 변경됨 |
| LWG 4025 | C++23 |
오버로드
(
7
)
가
E
가 이동 생성 가능하지 않거나
이동 할당 가능하지 않은 경우 삭제된 것으로 정의됨 |
이 경우 오버로드 해결에
참여하지 않음 |
참고 항목
|
기대값을 제자리에서 생성합니다
(public member function) |