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

来自cppreference.com
 
 
 
 
constexpr ~expected();
(C++23 起)

主模板析构函数

销毁当前包含的值:

  • 如果 has_value()true,那么就会销毁预期值。
  • 否则会销毁非预期值。

此析构函数是平凡的,如果 std::is_trivially_destructible_v<T>std::is_trivially_destructible_v<E> 都是 true

void 部分特化析构函数

如果 has_value()false,那么就会销毁非预期值。

如果 std::is_trivially_destructible_v<E>true,那么此析构函数是平凡的。

示例

#include <expected>
#include <iostream>
#include <source_location>

inline
void name(int x, std::source_location sloc = std::source_location::current())
{
    std::cout << sloc.function_name() << " : " << x << '\n';
}

struct Value
{
    int m{};
    ~Value() { name(m); }
};

struct Error
{
    int e{};
    ~Error() { name(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::~Error : 37
Error::~Error : 13
Value::~Value : 42
Morty Proxy This is a proxified and sanitized view of the page, visit original site.