std::expected 提供更安全的错误处理机制,通过封装值或错误信息避免异常和错误码,需显式检查结果并支持链式调用,如 divide(10, 2).and_then(...) 处理连续操作。
在C++23中,std::expected 被正式引入标准库,用于更安全、清晰地处理可能失败的操作。它借鉴了函数式编程语言中的思想(如Haskell的Either类型或Rust的Result),通过封装“期望值”或“错误信息”,替代传统的异常抛出或错误码返回方式。
std::expectedstd::optional 不同的是,它不仅能表达“有没有值”,还能说明“为什么没有值”。
例如:
#include#include std::expected
divide(int a, int b) { if (b == 0) { return std::unexpected("除数不能为零"); } return a / b; }
这里返回的是一个可能包含整数结果,也可能包含字符串错误的信息,调用者必须显式处理两种情况。
使用 std::expected 的关键是避免隐式假设操作成功。你需要检查其状态,并分别处理成功和失败路径。
常见操作包括:
示例:安全地链式处理多个可能失败的操作
auto result = divide(10, 2)
.and_then([](int x) -> std::expected {
if (x < 5) return std::unexpected("结果太小");
return x * 2;
});
if (result.has_value()) {
std::cout << "结果: " << *result << "\n";
} else {
std::cout << "出错: " << result.error() << "\n";
}
std::expected 支持类似函子(Functor)和单子(Monad)的操作,允许你以声明式方式组合操作流程,而不需要嵌套 if 判断或 try-catch 块。
典型模式:
举例:连续处理可能出错的步骤
std::expectedparse_and_sqrt(const std::string& s) { try { auto val = std::stod(s); if (val < 0) return std::unexpected("负数无法开方"); return std::sqrt(val); } catch (...) { return std::unexpected("解析失败"); } } // 使用 transform 简化处理 auto chain = parse_and_sqrt("4.0") .transform([](double x) { return x + 1; }) .or_else([](const std::string&) { return std::expected
(2.0); }); std::cout << "最终结果: " << *chain << "\n"; // 输出 3.0
尽管 std::expected 很强大,但在实际使用中需要注意以下几点:
.value(),这会退化成异常机制,失去预期优势std::errc 或自定义错误码提高一致性基本上就这些。std::expected 让错误处理变
得更直观、更安全,也推动 C++ 向更现代、更函数式的风格演进。合理使用它,能显著减少 bug 并提升代码可维护性。