std::expected<T,E>::~expected
来自cppreference.com
| |
(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