Namespaces
Variants

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

From cppreference.net
Utilities library
constexpr ~expected ( ) ;
(C++23부터)

메인 템플릿 소멸자

포함된 값을 파괴합니다:

  • 만약 has_value() true 이면, 기대값을 파괴합니다.
  • 그렇지 않으면, 예상치 못한 값을 파괴합니다.

이 소멸자는 std:: is_trivially_destructible_v < T > std:: is_trivially_destructible_v < E > 가 모두 true 인 경우 trivial합니다.

void 부분 특수화 소멸자

만약 has_value() false 인 경우, 예상치 못한 값을 파괴합니다.

이 소멸자는 std:: is_trivially_destructible_v < E > true 일 때 trivial합니다.

예제

#include <expected>
#include <print>
void info(auto name, int x)
{
    std::println("{} : {}", name, x);
}
struct Value
{
    int o{};
    ~Value() { info(__func__, o); }
};
struct Error
{
    int e{};
    ~Error() { info(__func__, e); }
};
int main()
{
    std::expected<Value, Error> e1{42};
    std::expected<Value, Error> e2{std::unexpect, 13};
    std::expected<void, Error> e3{std::unexpect, 37};
}

출력:

~Error : 37
~Error : 13
~Value : 42