Namespaces
Variants

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

From cppreference.net
Utilities library
기본 템플릿
constexpr expected ( ) ;
(1) (C++23부터)
constexpr expected ( const expected & other ) ;
(2) (C++23부터)
constexpr expected ( expected && other ) noexcept ( /* see below */ ) ;
(3) (C++23부터)
template < class U, class G >
constexpr expected ( const expected < U, G > & other ) ;
(4) (C++23부터)
(조건부 explicit)
template < class U, class G >
constexpr expected ( expected < U, G > && other ) ;
(5) (C++23부터)
(조건부 explicit)
template < class U = std:: remove_cv_t < T > >
constexpr explicit ( ! std:: is_convertible_v < U, T > ) expected ( U && v ) ;
(6) (C++23 이후)
template < class G >

constexpr explicit ( ! std:: is_convertible_v < const G & , E > )

expected ( const std:: unexpected < G > & e ) ;
(7) (C++23 이후)
template < class G >

constexpr explicit ( ! std:: is_convertible_v < G, E > )

expected ( std:: unexpected < G > && e ) ;
(8) (C++23 이후)
template < class ... Args >
constexpr explicit expected ( std:: in_place_t , Args && ... args ) ;
(9) (C++23 이후)
template < class U, class ... Args >

constexpr explicit
expected ( std:: in_place_t ,

std:: initializer_list < U > il, Args && ... args ) ;
(10) (C++23 이후)
template < class ... Args >
constexpr explicit expected ( std:: unexpect_t , Args && ... args ) ;
(11) (C++23 이후)
template < class U, class ... Args >

constexpr explicit
expected ( std:: unexpect_t ,

std:: initializer_list < U > il, Args && ... args ) ;
(12) (C++23 이후)
void 부분 특수화
constexpr expected ( ) ;
(13) (C++23부터)
constexpr expected ( const expected & other ) ;
(14) (C++23 이후)
constexpr expected ( expected && other )
noexcept ( std:: is_nothrow_move_constructible_v < E > ) ;
(15) (C++23 이후)
template < class U, class G >

constexpr explicit ( ! std:: is_convertible_v < const G & , E > )

expected ( const expected < U, G > & other ) ;
(16) (C++23 이후)
template < class U, class G >

constexpr explicit ( ! std:: is_convertible_v < G, E > )

expected ( expected < U, G > && other ) ;
(17) (C++23부터)
template < class G >

constexpr explicit ( ! std:: is_convertible_v < const G & , E > )

expected ( const std:: unexpected < G > & e ) ;
(18) (C++23 이후)
template < class G >

constexpr explicit ( ! std:: is_convertible_v < G, E > )

expected ( std:: unexpected < G > && e ) ;
(19) (C++23 이후)
constexpr explicit expected ( std:: in_place_t ) ;
(20) (C++23 이후)
template < class ... Args >
constexpr explicit expected ( std:: unexpect_t , Args && ... args ) ;
(21) (C++23 이후)
template < class U, class ... Args >

constexpr explicit
expected ( std:: unexpect_t ,

std:: initializer_list < U > il, Args && ... args ) ;
(22) (C++23 이후)

새로운 expected 객체를 생성합니다.

목차

매개변수

other - 다른 expected 객체로, 포함된 값이 복사됨
e - std::unexpected 객체로, 포함된 값이 복사됨
v - 포함된 값을 초기화하는 데 사용되는 값
args - 포함된 값을 초기화하는 데 사용되는 인수들
il - 포함된 값을 초기화하는 데 사용되는 초기화 리스트

효과

기본 템플릿 생성자

오버로드 초기화
방식
초기화 대상... has_value()
생성 후 상태
기대값 예상치 못한 값
( 1 ) (비어 있음) - true
( 2 ) 직접 초기화
(비-리스트)
* other other. error ( ) other. has_value ( )
  • 만약 true 인 경우, 예상 값만 초기화합니다.
  • 만약 false 인 경우, 예상하지 않은 값만 초기화합니다.
( 3 ) std :: move ( * other ) std :: move ( other. error ( ) )
( 4 ) std:: forward < const U & >
( * other )
std:: forward < const G & >
( other. error ( ) )
( 5 ) std:: forward < U > ( * other ) std:: forward < G >
( other. error ( ) )
( 6 ) std:: forward < U > ( v ) - true
( 7 ) - std:: forward < const G & >
( e. error ( ) )
false
( 8 ) std:: forward < G > ( e. error ( ) )
( 9 ) std:: forward < Args > ( args ) ... - true
( 10 ) il,
std:: forward < Args > ( args ) ...
( 11 ) - std:: forward < Args > ( args ) ... false
( 12 ) il,
std:: forward < Args > ( args ) ...

void 부분 특수화 생성자

오버로드 초기화 방식 예상치 못한 값(unexpected value)에 대한 초기화자 has_value() 생성 후 값
( 13 ) 해당 없음 - true
( 14 ) 직접 초기화 (비리스트) rhs. error ( ) other. has_value ( )
  • 만약 false 인 경우, 예상치 못한 값을 초기화합니다.
( 15 ) std :: move ( rhs. error )
( 16 ) std:: forward < const G & > ( rhs. error ( ) )
( 17 ) std:: forward < G > ( rhs. error ( ) )
( 18 ) std:: forward < const G & > ( e. error ( ) ) false
( 19 ) std:: forward < G > ( e. error ( ) )
( 20 ) 해당 없음 - true
( 21 ) 직접 초기화 (비리스트) std:: forward < Args > ( args ) ... false
( 22 ) il, std:: forward < Args > ( args ) ...

제약 조건 및 보충 정보

기본 템플릿 생성자

1) 이 오버로드는 다음 조건이 std:: is_default_constructible_v < T > 일 때만 오버로드 해결에 참여합니다.
2) 이 생성자는 std:: is_copy_constructible_v < T > std:: is_copy_constructible_v < E > 가 모두 true 인 경우를 제외하고 삭제된 것으로 정의됩니다.
이 생성자는 std:: is_trivially_copy_constructible_v < T > std:: is_trivially_copy_constructible_v < E > 가 모두 true 일 때 trivial입니다.
3) 이 오버로드는 다음 조건이 모두 std:: is_move_constructible_v < T > std:: is_move_constructible_v < E > true 일 때만 오버로드 해결에 참여합니다.
이 생성자는 std:: is_trivially_move_constructible_v < T > std:: is_trivially_move_constructible_v < E > 가 모두 true 일 때 trivial합니다.
4,5) These overloads participate in overload resolution only if all following conditions are satisfied:
4) 이 생성자는 explicit 입니다, 만약 std:: is_convertible_v < const U & , T > 또는 std:: is_convertible_v < const G & , E > false 인 경우.
5) 이 생성자는 explicit 입니다, 만약 std:: is_convertible_v < U, T > 또는 std:: is_convertible_v < G, E > false 인 경우.
6) 이 오버로드는 다음의 모든 조건이 만족될 때에만 오버로드 해결에 참여합니다:
7) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, const G & > 일 때만 오버로드 해결에 참여합니다.
8) 이 오버로드는 std:: is_constructible_v < E, G > true 인 경우에만 오버로드 해결에 참여합니다.
9) 이 오버로드는 다음 조건이 std:: is_constructible_v < T, Args... > true 일 때만 오버로드 해결에 참여합니다.
10) 이 오버로드는 다음 조건이 std:: is_constructible_v < T, std:: initializer_list < U > & , Args... > true 인 경우에만 오버로드 해결에 참여합니다.
11) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, Args... > true 일 때만 오버로드 해결에 참여합니다.
12) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, std:: initializer_list < U > & , Args... > true 일 때만 오버로드 해결에 참여합니다.

void 부분 특수화 생성자

14) 이 생성자는 std:: is_copy_constructible_v < E > true 인 경우를 제외하고 삭제된 것으로 정의됩니다.
이 생성자는 std:: is_trivially_copy_constructible_v < E > true 일 때 trivial입니다.
15) 이 오버로드는 다음 조건이 std:: is_move_constructible_v < E > true 인 경우에만 오버로드 해결에 참여합니다.
이 생성자는 std:: is_trivially_move_constructible_v < E > true 일 때 trivial입니다.
16,17) 다음 조건들이 모두 만족될 때만 이 오버로드들이 오버로드 해결에 참여합니다:
18) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, const G & > 일 때만 오버로드 해결에 참여합니다.
19) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, G > true 인 경우에만 오버로드 해결에 참여합니다.
21) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, Args... > 일 때에만 오버로드 해결에 참여합니다.
22) 이 오버로드는 다음 조건이 std:: is_constructible_v < E, std:: initializer_list < U > & , Args... > 일 때만 오버로드 해결에 참여합니다.

예외

기본 템플릿 생성자

1) 기대값의 초기화에서 발생하는 모든 예외를 throw합니다.
2) 기대값 또는 예상치 못한 값의 초기화 과정에서 발생하는 모든 예외를 던집니다.
3) 기대값 또는 예상치 못한 값의 초기화 과정에서 발생하는 모든 예외를 던집니다.
4,5) expected 값 또는 unexpected 값의 초기화 과정에서 발생하는 모든 예외를 throw합니다.
6) 기대값의 초기화에서 발생하는 모든 예외를 throw합니다.
7,8) 예상치 못한 값의 초기화에서 발생하는 모든 예외를 throw합니다.
9,10) 기대값 초기화에서 발생하는 모든 예외를 전파합니다.
11,12) 예기치 않은 값의 초기화에서 발생하는 모든 예외를 throw합니다.

void 부분 특수화 생성자

14-19) unexpected 값의 초기화 과정에서 발생하는 모든 예외를 전파합니다.
21,22) 예상치 못한 값의 초기화 과정에서 발생하는 모든 예외를 전파합니다.

예제

결함 보고서

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

DR 적용 대상 게시된 동작 올바른 동작
LWG 3886 C++23 overload 의 기본 템플릿 인수 ( 6 ) T 였음 std:: remove_cv_t < T > 로 변경됨

참고 항목

(C++23)
예상치 못한 값으로 표현됨
(클래스 템플릿)
인플레이스 생성 태그
(태그)
expected 에서 예상치 못한 값에 대한 인플레이스 생성 태그
(태그)