C++11/14/17 std::expected 与功能式扩展
std::expected 的单一头文件实现具有函数式扩展。Clang + GCC: MSVC: 在 Vcpkg、Conan 和 build2 上均可使用。std::expected 建议作为表示将具有预期值或出现异常的值的对象的首选方式。不幸的是,连接多个可能失败的计算可能会很繁琐,因为错误检查代码会混入实际编程逻辑中。此实现提供了一系列实用程序,可使使用 expected 的编码更清晰。例如,不需要写下以下代码:
std::expected<image,fail_reason> get_cute_cat (const image& img) {
auto cropped = crop_to_cat(img);
if (!cropped) {
return cropped;
}
auto with_tie = add_bow_tie(*cropped);
if (!with_tie) {
return with_tie;
}
auto with_sparkles = make_eyes_sparkle(*with_tie);
if (!with_sparkles) {
return with_sparkles;
}
return add_rainbow(make_smaller(*with_sparkles));
}
``` 你可以这样做:暂无开放 Issues,或尚未同步最近议题。