expected_unexpected
expected_unexpected (예상치 못한 값)
std::unexpected는 std::expected에 저장되는 예상치 못한 값을 나타내는 클래스 템플릿이에요. 특히 std::expected는 std::unexpected를 단일 인자로 받는 생성자를 제공해서, 예상치 못한 값을 포함하는 expected 객체를 만들 수 있어요. 비객체 타입, 배열 타입, std::unexpected의 특수화, 또는 cv 한정 타입으로 unexpected를 인스턴스화하면 프로그램은 ill-formed가 돼요.
출처: cppreference
본문
헤더
<expected> 헤더에 정의됨 |
||
|---|---|---|
template < class E > class unexpected ; |
(C++23부터) |
템플릿 매개변수
E |
- | 예상치 못한 값의 타입이에요. 이 타입은 배열 타입, 비객체 타입, std::unexpected의 특수화, 또는 cv 한정 타입이면 안 돼요. |
|---|
멤버 함수
| (생성자) | unexpected 객체를 생성해요 (공개 멤버 함수) |
|---|---|
| (소멸자) (암시적으로 선언됨) | 저장된 값과 함께 unexpected 객체를 파괴해요 (공개 멤버 함수) |
operator= (암시적으로 선언됨) |
저장된 값을 할당해요 (공개 멤버 함수) |
error |
저장된 값에 접근해요 (공개 멤버 함수) |
swap |
저장된 값을 교환해요 (공개 멤버 함수) |
비멤버 함수
operator== (C++23) |
저장된 값을 비교해요 (함수 템플릿) |
|---|---|
swap (std::unexpected) (C++23) |
std::swap 알고리즘을 특수화해요 (함수 템플릿) |
std::unexpected::unexpected
constexpr unexpected ( const unexpected & ) = default ; // (1)
constexpr unexpected ( unexpected && ) = default ; // (2)
template < class Err = E > constexpr explicit unexpected ( Err && e ); // (3)
template < class ... Args > constexpr explicit unexpected ( std :: in_place_t , Args && ... args ); // (4)
template < class U , class ... Args > constexpr explicit unexpected ( std :: in_place_t , std :: initializer_list < U > il , Args && ... args ); // (5)
std::unexpected 객체를 생성해요.
- 이 오버로드는
std :: is_same_v < std :: remove_cvref_t < Err > , unexpected >가false이고,std :: is_same_v < std :: remove_cvref_t < Err > , std :: in_place_t >가false이며,std :: is_constructible_v < E , Err >가true인 경우에만 오버로드 해석에 참여해요. - 이 오버로드는
std :: is_constructible_v < E , Args ... >가true인 경우에만 오버로드 해석에 참여해요. - 이 오버로드는
std :: is_constructible_v < E , std :: initializer_list < U >& , Args ... >가true인 경우에만 오버로드 해석에 참여해요.
매개변수
e |
- | 포함된 값을 초기화할 값이에요. |
|---|---|---|
args... |
- | 포함된 값을 초기화할 인자들이에요. |
il |
- | 포함된 값을 초기화할 초기화자 목록이에요. |
예외
E의 생성자가 던지는 모든 예외를 던져요.
std::unexpected::error
constexpr const E & error () const & noexcept ;
constexpr E & error () & noexcept ;
constexpr const E && error () const && noexcept ;
constexpr E && error () && noexcept ;
저장된 값에 대한 참조를 반환해요.
std::unexpected::swap
constexpr void swap ( unexpected & other ) noexcept ( std :: is_nothrow_swappable_v < E > );
저장된 값을 std :: swap ; swap ( error (), other . error ());을 사용하는 것처럼 교환해요.
std :: is_swappable_v < E >가 false이면 프로그램은 ill-formed예요.
operator== (std::unexpected)
template < class E2 > friend constexpr bool operator == ( unexpected & x , std :: unexpected < E2 >& y );
저장된 값을 return x . error () == y . error ()처럼 비교해요.
x . error () == e . error () 표현식이 잘 구성되지 않거나, 그 결과가 bool로 변환 가능하지 않으면 프로그램은 ill-formed예요.
이 함수는 일반적인 비한정 또는 한정 조회로는 보이지 않으며, std::unexpected<E>가 인자의 연관 클래스일 때 인자 의존적 조회로만 찾을 수 있어요.
swap (std::unexpected)
friend constexpr void swap ( unexpected & x , unexpected & y ) noexcept ( noexcept ( x . swap ( y )));
x . swap ( y )와 동일해요.
이 오버로드는 std :: is_swappable_v < E >가 true인 경우에만 오버로드 해석에 참여해요.
이 함수는 일반적인 비한정 또는 한정 조회로는 보이지 않으며, std::unexpected<E>가 인자의 연관 클래스일 때 인자 의존적 조회로만 찾을 수 있어요.
추론 가이드
template < class E > unexpected ( E ) -> unexpected < E > ; // (C++23부터)
unexpected에 대해 생성자 인자로부터 추론할 수 있도록 추론 가이드가 제공돼요.
참고
C++17 이전에는 std::unexpected라는 이름이 동적 예외 명세가 위반되었을 때 C++ 런타임이 호출하는 함수를 나타냈어요.
예제
#include <expected>
#include <iostream>
enum class error
{
compile_time_error,
runtime_error
};
[[nodiscard]] auto unexpected_runtime_error() -> std::expected<int, error>
{
return std::unexpected(error::runtime_error);
}
int main()
{
std::expected<double, int> ex = std::unexpected(3);
if (!ex)
std::cout << "ex contains an error value\n";
if (ex == std::unexpected(3))
std::cout << "The error value is equal to 3\n";
const auto e = unexpected_runtime_error();
e.and_then([](const auto& e) -> std::expected<int, error>
{
std::cout << "and_then: " << int(e); // not printed
return {};
})
.or_else([](const auto& e) -> std::expected<int, error>
{
std::cout << "or_else: " << int(e); // prints this line
return {};
});
}
출력:
ex contains an error value
The error value is equal to 3
or_else: 1
같이 보기
| (생성자) | expected 객체를 생성해요 (공개 멤버 함수) [edit] |
|---|---|
operator== (C++23) |
expected 객체를 비교해요 (함수 템플릿) [edit] |