std::expected<T, E> 是C++23引入的模板类,用于表示操作成功时返回值T或失败时返回错误E,替代异常或错误码。它比optional多出错误信息,支持has_value、value、error等方法,并可结合and_then实现链式错误处理,提升代码健壮性与可读性。

从C++23开始,std::expected 被正式纳入标准库,提供了一种更安全、更明确的错误处理方式。它用来表示一个操作可能成功返回值,也可能失败并返回错误信息,替代传统的异常或返回码机制。
什么是 std::expected?
std::expected<T, E> 是一个类模板,表示“期望得到一个类型为 T 的值,否则得到一个类型为 E 的错误”。它类似于函数式语言中的 Either 类型,但专为“结果或错误”场景设计。
与 std::optional<T> 不同的是:optional 表示“有值或无值”,而 expected 还能告诉你“为什么没有值”。
基本用法示例
假设我们要写一个除法函数,除零时返回错误码:
立即学习“C++免费学习笔记(深入)”;
#include <expected>
#include <iostream>
#include <string>
std::expected<double, std::string> divide(double a, double b) {
if (b == 0.0) {
return std::unexpected("Division by zero");
}
return a / b;
}
调用该函数:
auto result = divide(10, 3);
if (result.has_value()) {
std::cout << "Result: " << result.value() << "\n";
} else {
std::cout << "Error: " << result.error() << "\n";
}
也可以用结构化绑定(如果支持)或直接解包:
auto result = divide(10, 0);
if (result) {
std::cout << "Success: " << *result << "\n";
} else {
std::cout << "Failed: " << result.error() << "\n";
}
如何编译使用 C++23 和 std::expected
目前主流编译器对 std::expected 的支持需要开启 C++23 模式,并使用较新版本的 STL(如 libstdc++ 或 libc++)。
- 使用 GCC(建议 13+):
g++ -std=c++23 -Wall -Wextra source.cpp
- Clang(建议 17+)配合 libc++:
clang++ -std=c++23 -stdlib=libc++ source.cpp
注意:GCC 13 中 libstdc++ 对 std::expected 的支持是实验性的,需定义宏启用:
-D_GLIBCXX_USE_CXX11_ABI=1 -fconcepts
或者使用第三方实现(如 TL::expected)在旧版本中练习语法。
链式错误处理与 map / and_then
std::expected 支持类似 monadic 的操作,便于组合多个可能出错的操作。
例如使用 and_then 实现链式调用:
std::expected<int, std::string> to_int(const std::string& s) {
try {
return std::stoi(s);
} catch (...) {
return std::unexpected("Invalid number");
}
}
auto process(const std::string& str) {
return to_int(str)
.and_then([](int n) -> std::expected<int, std::string> {
if (n == 0) return std::unexpected("Zero not allowed");
return 100 / n;
});
}
若任意一步失败,后续不会执行,直接传递错误。
基本上就这些。std::expected 让错误处理变得更清晰、更少依赖异常或全局状态,适合现代 C++ 中追求健壮性和可读性的项目。











