Namespaces
Variants

std::expected<T, E>::operator=

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐지됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
Swap 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
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 객체에 새 값을 할당합니다.

1,2) 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-한정된) void
      • std::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-한정된) void
      • std::is_nothrow_move_constructible_v<T>
      • std::is_nothrow_move_constructible_v<E>
3) 예상 값으로부터 할당합니다.
  • 만약 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>
4,5) 예상치 못한 값으로부터 할당합니다.

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-한정된) void
      • std::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 의 초기화자로 사용되는 인수들

반환값

1-9) * this

예외

예제

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 수정된 동작
LWG 3886 C++23 오버로드 ( 3 ) 의 기본 템플릿 인수가 T 였음 std:: remove_cv_t < T > 로 변경됨
LWG 4025 C++23 오버로드 ( 7 ) 가 E 가 이동 생성 가능하지 않거나
이동 할당 가능하지 않은 경우 삭제된 것으로 정의됨
이 경우 오버로드 해결에
참여하지 않음

참고 항목

기대값을 제자리에서 생성합니다
(public member function)