C++23中的std::expected如何处理错误?(类型安全的异常返回值处理)

std::expected是C++23引入的值语义错误传播机

制,不抛异常,以持有T或E的可变容器形式将错误视为可检查的合法状态,适用于需避免栈展开、精细控制错误流向的场景。

std::expected 是什么,它不替代 throw/catch

std::expected 是 C++23 引入的值语义错误传播机制,本质是一个持有 T(成功值)或 E(错误值)的可变容器。它**不抛异常**,也不捕获异常,而是把“出错”当作一种合法、可检查的数据状态来处理。适合那些你明确不想触发栈展开、需要细粒度控制错误流向的场景,比如系统调用封装、协议解析、配置加载。

如何从函数返回并检查 std::expected

典型模式是函数直接返回 std::expectedstd::expected<:string std::string> 这类类型。调用方必须显式检查是否含值:

std::expected parse_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) { ... })
  • 所有这些操作都保持值语义,无异常、无动态内存分配(除非你的 TE 自身有)

和 std::optional、status code、异常混用时的坑

std::expected 不是万能胶。它和其它错误处理机制共存时容易混淆语义:

  • 别用 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::expectederror() 成员函数在 has_value() == true 时行为未定义 —— 不是返回垃圾值,而是直接 UB。检查必须前置。