std::expected是C++23引入的值语义错误传播机制,不抛异常,以持有T或E的可变容器形式将错误视为可检查的合法状态,适用于需避免栈展开、精细控制错误流向的场景。

std::expected 是什么,它不替代 throw/catch
std::expected 是 C++23 引入的值语义错误传播机制,本质是一个持有 T(成功值)或 E(错误值)的可变容器。它**不抛异常**,也不捕获异常,而是把“出错”当作一种合法、可检查的数据状态来处理。适合那些你明确不想触发栈展开、需要细粒度控制错误流向的场景,比如系统调用封装、协议解析、配置加载。
如何从函数返回并检查 std::expected
典型模式是函数直接返回 std::expected 或 std::expected<:string std::string> 这类类型。调用方必须显式检查是否含值:
std::expectedparse_int(const char* s) { char* end; long v = std::strtol(s, &end, 10); if (*end != '\0' || errno == ERANGE) { return std::unexpected(std::errc::invalid_argument); } return static_cast (v); } auto result = parse_int("42"); if (result) { std::cout << "got: " << *result << "\n"; // OK } else { std::cout << "error: " << std::make_error_code(result.error()).message() << "\n"; }
- 用
if (result)检查是否为预期值(即has_value()) - 用
*result解包成功值(若未检查直接解包会std::terminate) - 用
result.error()获取错误对象,注意它返回的是E&,不是拷贝 - 不要对
std::expected做*result—— 它没有值,只能用result.has_value()或!result
链式调用:and_then 和 or_else 的实际用途
当多个可能失败的操作需顺序执行时,and_then 可避免嵌套 if;而 or_else 用于提供备选路径(类似“默认值”或“fallback 函数”):
auto read_config() -> std::expected{ /* ... */ } auto validate(Config c) -> std::expected { /* ... */ } auto pipeline = read_config() .and_then(validate) .and_then([](ValidConfig c) { return launch_service(c) ? std::expected
{} : std::unexpected("launch failed"); }); if (!pipeline) { std::cerr << "Pipeline error: " << pipeline.error() << "\n"; }
-
and_then接收一个返回std::expected的函数,自动扁平化嵌套结果(类似std::optional::transform+std::expected合并逻辑) -
or_else在当前为 error 时被调用,参数是E&,返回另一个std::expected—— 注意它不接收std::unexpected,别写成or_else([](auto u) { ... }) - 所有这些操作都保持值语义,无异常、无动态内存分配(除非你的
T或E自身有)
和 std::optional、status code、异常混用时的坑
std::expected 不是万能胶。它和其它错误处理机制共存时容易混淆语义:
立即学习“C++免费学习笔记(深入)”;
- 别用
std::expected模拟异常 —— 这违背了它的设计初衷,且丢失了栈信息;真要转发异常,请用std::exception_ptr+ 显式std::rethrow_exception - 不要把
std::expected当作std::optional用:后者表示“不存在”,前者表示“存在但失败”,语义不同 - 与 C API 交互时,常见错误是把
errno直接塞进E类型却不映射为标准错误码类别(如std::errc),导致std::error_code(e).message()返回空字符串 - 移动语义要注意:
std::expected移动后源对象进入未指定状态(C++23 标准未强制要求置空),不要在移动后访问其value()或error()
最常被忽略的一点:std::expected 的 error() 成员函数在 has_value() == true 时行为未定义 —— 不是返回垃圾值,而是直接 UB。检查必须前置。










