Boost C++ 库

……是世界上备受推崇、设计精湛的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ 编码标准

主页 - Boost.Outcome 文档 - Boost C++ 函数库
HomeNext

Outcome 2.2 库

Niall Douglas

根据 Boost 软件许可证 1.0 版分发。(请参阅随附的 LICENSE_1_0.txt 文件或访问 https://boost.ac.cn/LICENSE_1_0.txt 获取副本)

介绍

注意

2021 年 12 月底,Standalone Outcome 获得了保证的未来 ABI 稳定性。从 v2.2.3 开始,您将获得跨 Outcome 版本的所有 ABI 兼容性保证。

Outcome 是一套用于在**不适合直接使用 C++ 异常处理**的场景下报告和处理函数失败的工具。此类场景包括:

Outcome 通过从函数返回一个特殊类型来处理失败,该类型能够存储成功计算的值(或 `void`)或失败信息。Outcome 还附带了一套处理此类类型的惯用法。

特别注意确保 Outcome 对构建时间的影响尽可能小,使其适用于非常大的代码库的全局头文件中。`result` 的存储布局已获保证且与 C 兼容1,这使得基于 Outcome 的代码具有长期的 ABI 稳定性。

Outcome 对完全确定的、全部 `noexcept` 的 C++ 协程支持尤其强大,我们提供了 Outcome 优化的 eager<T, Executor = void>/atomic_eager<T, Executor = void>lazy<T, Executor = void>/atomic_lazy<T, Executor = void>generator<T, Executor = void> 协程,它们适用于任何用户类型。

示例用法(C++)

Outcome 库中的主要工作是 `result`:它代表类型为 `T` 的成功计算值,或代表失败原因的 `std::error_code`/`boost::system::error_code`2。您在函数的返回类型中使用它。

outcome::result<string> data_from_file(string_view path) noexcept;
在 Github 上查看此代码

可以手动检查状态

if (outcome::result<string> rslt = data_from_file("config.cfg"))
  use_string(rslt.value());                   // returns string
else
  throw LibError{rslt.error(), "config.cfg"}; // returns error_code
在 Github 上查看此代码

或者,如果此函数在另一个也返回 `result` 的函数中调用,您可以使用专门的控制语句。

outcome::result<int> process(const string& content) noexcept;

outcome::result<int> int_from_file(string_view path) noexcept
{
  BOOST_OUTCOME_TRY(auto str, data_from_file(path));
  // if control gets here data_from_file() has succeeded
  return process(str);  // decltype(str) == string
}
在 Github 上查看此代码

BOOST_OUTCOME_TRY 是一个控制语句。如果返回的 `result` 对象包含错误信息,则当前函数将立即返回,并带有包含相同失败信息的 `result`;否则,作用域内将可用类型为 `T` 的自动对象。

示例用法(C)

等同于 C++ API:BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM(ident, T) 声明 C 类型,之后 BOOST_OUTCOME_C_RESULT_SYSTEM(ident) 指代它。您在函数的返回类型中使用它。

BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM(result_string, const char *)

BOOST_OUTCOME_C_RESULT_SYSTEM(result_string) data_from_file(const char *path);
在 Github 上查看此代码

可以手动检查状态

  BOOST_OUTCOME_C_RESULT_SYSTEM(result_string) rslt = data_from_file("config.cfg");
  if(BOOST_OUTCOME_C_RESULT_HAS_VALUE(rslt))
    use_string(rslt.value);  // returns string
  else
    fprintf(stderr, "%s\n", outcome_status_code_message(&rslt.error));
在 Github 上查看此代码

或者,如果此函数在另一个也返回 BOOST_OUTCOME_C_RESULT_SYSTEM(ident) 的函数中调用,您可以使用专门的控制语句。

BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM(result_int, int)

BOOST_OUTCOME_C_RESULT_SYSTEM(result_int) process(const char *content);

BOOST_OUTCOME_C_RESULT_SYSTEM(result_int) int_from_file(const char *path)
{
  BOOST_OUTCOME_C_RESULT_SYSTEM_TRY(const char *str, result_int, /* cleanup on fail */, data_from_file(path));
  // if control gets here data_from_file() has succeeded
  return process(str);  // decltype(str) == string
}
在 Github 上查看此代码

C Result 的布局保证与其 C++ 等效项完全相同。提供了方便的转换函数,但您也可以使用 `reinterpret_cast`。

注意

该库于 1.70 版本(2019 年春季)加入了 Boost C++ 库。如果需要,可以将其移植到更早的 Boost 版本中

  1. 如果您选择 C 兼容的 `T` 和 `E` 类型。[返回]
  2. `result` 在 Standalone Outcome 中默认为 `std::error_code`,在 Boost.Outcome 中默认为 `boost::system::error_code`。您可以使用 `std_result` 或 `boost_result` 来强制指定选择。[返回]

最后修订:2024 年 7 月 16 日,21:33:35 +0100


Prev HomeNext