Boost C++ 库

...世界上评价最高、设计最专业的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

ADL 桥接 - Boost.Outcome 文档 - Boost C++ 函数库
Prev Up HomeNext

ADL 桥接

注意

在 Outcome v2.2 中,基于 ADL 的事件钩子被基于策略的事件钩子(下一页)取代。本节中的代码在 v2.2 及更高版本中仍然有效,只是 ADL 不再用于查找钩子。

在上一节中,我们使用了 failure_info 类型来创建 ADL 桥接,连接到 ADL 能够发现 outcome_throw_as_system_error_with_payload() 函数的命名空间。

在这里,我们做同样的事情,但更直接,通过在本地命名空间创建一个 std::error_code 的薄克隆。这确保了在发现事件钩子时(仅限 Outcome v2.1 及更早版本),编译器会搜索此命名空间。

namespace error_code_extended
{
  // Use the error_code type as the ADL bridge for the hooks by creating a type here
  // It can be any type that your localised result uses, including the value type but
  // by localising the error code type here you prevent nasty surprises later when the
  // value type you use doesn't trigger the ADL bridge.
  struct error_code : public std::error_code
  {
    // literally passthrough
    using std::error_code::error_code;
    error_code() = default;
    error_code(std::error_code ec)
        : std::error_code(ec)
    {
    }
  };

  // Localise result and outcome to using the local error_code so this namespace gets looked up for the hooks
  template <class R> using result = BOOST_OUTCOME_V2_NAMESPACE::result<R, error_code>;
  template <class R> using outcome = BOOST_OUTCOME_V2_NAMESPACE::outcome<R, error_code /*, std::exception_ptr */>;
}
在 Github 上查看此代码

为了方便起见,我们在与 ADL 桥接 error_code 绑定的此命名空间中,将 resultoutcome 的本地副本模板化为别名。

最后修改:2020 年 12 月 15 日,UTC 12:22:39

Prev Up HomeNext