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 の場合、自明です。

void 部分特殊化デストラクタ

has_value() false の場合、unexpected値を破棄します。

このデストラクタは、 std:: is_trivially_destructible_v < E > true の場合、トリビアルです。

#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