Namespaces
Variants

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

From cppreference.net
Utilities library
기본 템플릿
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부터)
template < class G >
constexpr expected & operator = ( const std:: unexpected < G > & e ) ;
(4) (C++23 이후)
template < class G >
constexpr expected & operator = ( std:: unexpected < G > && e ) ;
(5) (C++23 이후)
void 부분 특수화
constexpr expected & operator = ( const expected & other ) ;
(6) (C++23부터)
constexpr expected & operator = ( expected && other )
noexcept ( /* see below */ ) ;
(7) (C++23 이후)
template < class G >
constexpr expected & operator = ( const std:: unexpected < G > & e ) ;
(8) (C++23 이후)
template < class G >
constexpr expected & operator = ( std:: unexpected < G > && e ) ;
(9) (C++23 이후)
헬퍼 함수 템플릿
template < class T, class U, class ... Args >
constexpr void /*재초기화-예상*/ ( T & newval, U & oldval, Args && ... args )
(10) (C++23부터)
( 설명 전용* )

기존의 expected 객체에 새로운 값을 할당합니다.

목차

매개변수

other - 할당할 포함된 값을 가진 다른 expected 객체
v - 포함된 값에 할당할 값
e - std::unexpected 객체의 포함된 값을 할당
newval - 생성될 포함된 값
oldval - 소멸될 포함된 값
args - newval 의 초기화자로 사용되는 인수들

효과

기본 템플릿 할당 연산자

1,2) other 의 상태를 * this 에 할당합니다.
만약 has_value() rhs. has_value ( ) 가 서로 다른 값을 가지는 경우 (즉, * this other 중 하나는 기대값 val 을 포함하고 다른 하나는 예상치 못한 값 unex 을 포함하는 경우), 설명 전용 함수 템플릿 reinit-expected 이 상태를 안전하게 업데이트하기 위해 호출됩니다.
1) 포함된 값은 다음과 같이 할당됩니다:
has_value() 의 값 other. has_value ( ) 의 값
true false
true val = * other ; reinit-expected
( unex , val , other. error ( ) ) ;
false reinit-expected
( val , unex , * other ) ;
unex = other. error ( ) ;
2) 포함된 값은 다음과 같이 할당됩니다:
has_value() 의 값 other. has_value ( ) 의 값
true false
true val = std :: move ( * other ) ; reinit-expected
( unex , val , std :: move ( other. error ( ) ) ) ;
false reinit-expected
( val , unex ,
std :: move ( * other ) ) ;
unex = std :: move ( other. error ( ) ) ;
그런 다음, 예외가 발생하지 않았다면 has_val = other. has_value ( ) ; 를 실행합니다.
3) 기대값은 다음과 같이 할당됩니다:
has_value() 의 값 동등한 표현
true val = std:: forward < U > ( v ) ;
false reinit-expected ( val , unex , std:: forward < U > ( v ) ) ;
has_val = false ;
4,5) 예상치 못한 값은 다음과 같이 할당됩니다:
오버로드 has_value() 동등한 코드
( 4 ) true reinit-expected ( val , unex , std:: forward < const G & > ( e. error ( ) ) ) ;
has_val = false ;
false unex = std:: forward < const G & > ( e. error ( ) ) ;
( 5 ) true reinit-expected ( val , unex , std:: forward < G > ( e. error ( ) ) ) ;
has_val = false ;
false unex = std:: forward < G > ( e. error ( ) ) ;

void 부분 특수화 할당 연산자

6) 예상치 못한 값은 다음과 같이 할당되거나 파괴됩니다:
has_value() 의 값 other. has_value ( ) 의 값
true false
true (효과 없음) std:: construct_at
( std:: addressof ( unex ) , rhs. unex ) ;
has_val = false ;
false std:: destroy_at ( std:: addressof ( unex ) ) ;
has_val = true ;
unex = other. error ( ) ;
7) 예상치 못한 값은 다음과 같이 할당되거나 파괴됩니다:
has_value() 의 값 other. has_value ( ) 의 값
true false
true (효과 없음) std:: construct_at
( std:: addressof ( unex ) ,
std :: move ( rhs. unex ) ) ;
has_val = false ;
false std:: destroy_at ( std:: addressof ( unex ) ) ;
has_val = true ;
unex = std :: move ( other. error ( ) ) ;
8,9) 예상치 못한 값은 다음과 같이 할당됩니다:
오버로드 has_value() 동등한 표현
( 8 ) true std:: construct_at ( std:: addressof ( unex ) ,
std:: forward < const G & > ( e. error ( ) ) ) ;
has_val = false ;
false unex = std:: forward < const G & > ( e. error ( ) ) ;
( 9 ) true std:: construct_at ( std:: addressof ( unex ) , std:: forward < G > ( e. error ( ) ) ) ;
has_val = false ;
false unex = std:: forward < G > ( e. error ( ) ) ;

헬퍼 함수 템플릿

설명 전용 함수 템플릿 reinit-expected 는 다음과 같이 "정의"됩니다:

template<class NewType, class OldType, class... Args>
constexpr void reinit-expected(NewType& new_val, OldType& old_val, Args&&... args)
{
    // 경우 1: "new_val"의 생성이 예외를 던지지 않는 경우:
    // "old_val"을 파괴한 후 "new_val"을 직접 생성할 수 있음
    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)...);
    }
    // 경우 2: "new_val"의 이동 생성이 예외를 던지지 않는 경우:
    // 먼저 임시 NewType 객체를 생성
    // (이 생성 과정에서 예외가 발생하면 "old_val"은 그대로 유지됨)
    else if constexpr (std::is_nothrow_move_constructible_v<NewType>)
    {
        NewType temp(std::forward<Args>(args)...); // 예외를 던질 수 있음
        std::destroy_at(std::addressof(old_val));
        std::construct_at(std::addressof(new_val), std::move(temp));
    }
    // 경우 3: "new_val"의 생성이 예외를 던질 가능성이 있는 경우:
    // 예외로부터 복구하기 위해 "old_val"의 백업이 필요함
    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)...); // 예외를 던질 수 있음
        }
        catch (...)
        {
            std::construct_at(std::addressof(old_val), std::move(temp));
            throw;
        }
    }
}

이 함수 템플릿은 할당 연산이 * this 가 대체 값을 보유하게 될 때 호출됩니다 (즉, 기대 값에서 예상치 못한 값으로, 또는 예상치 못한 값에서 기대 값으로 변경될 때).

이 경우, 새로운 값 newval 을 생성하기 전에 기존 값 oldval 을 파괴해야 합니다. 그러나 newval 의 생성 과정에서 예외가 발생할 수 있습니다. 강력한 예외 안전성 보장 을 제공하기 위해서는 예외를 재발생시키기 전에 기존 값을 복원해야 하며, 이를 통해 예외가 처리되는 동안 * this 가 유효한 상태를 유지할 수 있습니다.

반환값

1-9) * this

제약 조건 및 보충 정보

기본 템플릿 할당 연산자

1) 다음 값들이 모두 true 가 아닌 경우, 이 오버로드는 삭제된 것으로 정의됩니다:
2) 이 오버로드는 다음의 모든 값들이 true 인 경우에만 오버로드 해결에 참여합니다:
3) 이 오버로드는 다음의 모든 조건이 만족될 때만 오버로드 해결에 참여합니다:
4) 이 오버로드는 다음의 모든 값들이 true 인 경우에만 오버로드 해결에 참여합니다:
5) 이 오버로드는 다음의 모든 값들이 true 인 경우에만 오버로드 해결에 참여합니다:

void 부분 특수화 할당 연산자

6) 이 오버로드는 std:: is_copy_assignable_v < E > std:: is_copy_constructible_v < E > 가 모두 true 인 경우를 제외하고 삭제된 것으로 정의됩니다.
7) 이 오버로드는 다음 조건이 모두 std:: is_move_constructible_v < E > std:: is_move_assignable_v < E > 가 모두 true 일 때만 오버로드 해결에 참여합니다.
8) 이 오버로드는 다음 조건이 모두 std:: is_constructible_v < E, const G & > std:: is_assignable_v < E & , const G & > 가 모두 true 인 경우에만 오버로드 해결에 참여합니다.
9) 이 오버로드는 다음 조건이 모두 std:: is_constructible_v < E, G > std:: is_assignable_v < E & , G > true 일 때만 오버로드 해결에 참여합니다.

예외

예제

결함 보고서

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

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

참고 항목

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