Namespaces
Variants

std:: unexpected

From cppreference.net
Utilities library
헤더에 정의됨 <expected>
template < class E >
class unexpected ;
(C++23부터)

클래스 템플릿 std::unexpected std::expected 에 저장된 예상치 못한 값을 나타냅니다. 특히, std::expected std::unexpected 를 단일 인자로 받는 생성자들을 가지며, 이를 통해 예상치 못한 값을 포함하는 expected 객체를 생성합니다.

프로그램이 비객체 타입, 배열 타입, std::unexpected 의 특수화, 또는 cv 한정 타입으로 unexpected 를 인스턴스화하는 경우 해당 프로그램은 형식이 잘못되었습니다.

목차

템플릿 매개변수

E - 예상치 못한 값의 타입. 해당 타입은 배열 타입, 비객체 타입, std::unexpected 의 특수화, 또는 cv 한정 타입이 아니어야 합니다.

멤버 함수

unexpected 객체를 생성합니다
(public 멤버 함수)
(소멸자)
(암시적으로 선언됨)
저장된 값과 함께 unexpected 객체를 파괴합니다
(public 멤버 함수)
operator=
(암시적으로 선언됨)
저장된 값을 할당합니다
(public 멤버 함수)
저장된 값에 접근합니다
(public 멤버 함수)
저장된 값을 교환합니다
(public 멤버 함수)

비멤버 함수

(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 객체를 생성합니다.

1,2) 복사/이동 생성자. 저장된 값을 각각 복사하거나 이동합니다.
3) 저장된 값을 마치 직접 초기화 를 통해 E 타입의 값을 std:: forward < Err > ( e ) 로부터 생성하는 것처럼 구성합니다.
4) 저장된 값을 구성합니다. 마치 직접 초기화 를 통해 E 타입의 값을 인수들 std:: forward < Args > ( args ) ... 로부터 초기화하는 것처럼.
5) 저장된 값을 마치 직접 초기화(direct-initializing) 를 통해 E 타입의 값을 인수들 il, std:: forward < Args > ( args ) ... 로부터 생성하는 것처럼 구성합니다.

매개변수

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 > ) ;

저장된 값을 교환합니다. 마치 using std:: swap ; swap ( error ( ) , other. error ( ) ) ; 와 같이 수행됩니다.

std:: is_swappable_v < E > 가 false인 경우 프로그램의 형식이 올바르지 않습니다.

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입니다.

이 함수는 일반적인 unqualified lookup 이나 qualified lookup 으로는 보이지 않으며, std::unexpected<E> 가 인자들의 associated class일 때만 argument-dependent lookup 에 의해 찾을 수 있습니다.

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 이후)

deduction guide 는 생성자 인수로부터의 추론을 허용하기 위해 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); // 출력되지 않음
        return {};
    })
    .or_else([](const auto& e) -> std::expected<int, error>
    {
        std::cout << "or_else: " << int(e); // 이 줄을 출력
        return {};
    });
}

출력:

ex contains an error value
The error value is equal to 3
or_else: 1

참고 항목

expected 객체를 생성합니다
(public member function)
기대값을 반환합니다
(public member function)
내용을 교환합니다
(public member function)
std::swap 알고리즘을 특수화합니다
(function)
(C++23)
expected 객체를 비교합니다
(function template)