std::expected<T, E> 提供更安全的错误处理机制,通过封装值或错误信息避免异常和错误码,需显式检查结果并支持链式调用,如 divide(10, 2).and_then(...) 处理连续操作。

在C++23中,std::expected 被正式引入标准库,用于更安全、清晰地处理可能失败的操作。它借鉴了函数式编程语言中的思想(如Haskell的Either类型或Rust的Result),通过封装“期望值”或“错误信息”,替代传统的异常抛出或错误码返回方式。
std::expected<T, E> 是一个模板类,表示一个操作的结果:要么成功并包含类型为 T 的值,要么失败并包含类型为 E 的错误信息。与 std::optional<T> 不同的是,它不仅能表达“有没有值”,还能说明“为什么没有值”。
例如:
#include <expected>
#include <iostream>
<p>std::expected<int, std::string> divide(int a, int b) {
if (b == 0) {
return std::unexpected("除数不能为零");
}
return a / b;
}</p>这里返回的是一个可能包含整数结果,也可能包含字符串错误的信息,调用者必须显式处理两种情况。
立即学习“C++免费学习笔记(深入)”;
使用 std::expected 的关键是避免隐式假设操作成功。你需要检查其状态,并分别处理成功和失败路径。
常见操作包括:
示例:安全地链式处理多个可能失败的操作
auto result = divide(10, 2)
.and_then([](int x) -> std::expected<int, std::string> {
if (x < 5) return std::unexpected("结果太小");
return x * 2;
});
<p>if (result.has_value()) {
std::cout << "结果: " << *result << "\n";
} else {
std::cout << "出错: " << result.error() << "\n";
}</p>std::expected 支持类似函子(Functor)和单子(Monad)的操作,允许你以声明式方式组合操作流程,而不需要嵌套 if 判断或 try-catch 块。
典型模式:
举例:连续处理可能出错的步骤
std::expected<double, std::string> parse_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("解析失败");
}
}
<p>// 使用 transform 简化处理
auto chain = parse_and_sqrt("4.0")
.transform([](double x) { return x + 1; })
.or_else([](const std::string&) { return std::expected<double, std::string>(2.0); });</p><p>std::cout << "最终结果: " << *chain << "\n"; // 输出 3.0</p>尽管 std::expected 很强大,但在实际使用中需要注意以下几点:
.value(),这会退化成异常机制,失去预期优势std::errc 或自定义错误码提高一致性基本上就这些。std::expected 让错误处理变得更直观、更安全,也推动 C++ 向更现代、更函数式的风格演进。合理使用它,能显著减少 bug 并提升代码可维护性。
以上就是C++怎么使用C++23的std::expected_C++现代错误处理与函数式编程思想的详细内容,更多请关注php中文网其它相关文章!
编程怎么学习?编程怎么入门?编程在哪学?编程怎么学才快?不用担心,这里为大家提供了编程速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号