Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
Prev Up HomeNext

Mapping the File I/O library into the Application

To handle the File I/O library, once again we turn to custom ValueOrError converters:

// Inject custom ValueOrError conversion
namespace convert
  // Provide custom ValueOrError conversion from filelib::result<U>
  // into any app::outcome<T>
  template <class T, class U>  //
  struct value_or_error<app::outcome<T>, filelib::result<U>>
    // True to indicate that this converter wants `result`/`outcome`
    // to NOT reject all other `result`
    static constexpr bool enable_result_inputs = true;
    // False to indicate that this converter wants `outcome` to NOT
    // reject all other `outcome`
    static constexpr bool enable_outcome_inputs = true;

    template <class X,                                                                              //
              typename = std::enable_if_t<std::is_same<filelib::result<U>, std::decay_t<X>>::value  //
                                          && std::is_constructible<T, U>::value>>                   //
    constexpr app::outcome<T> operator()(X &&src)
      // Forward any successful value
        return {std::forward<X>(src).value()};

      // Synthesise a filesystem_error, exactly as if someone had
      // called src.value()
      auto &fi = src.error();
      BOOST_OUTCOME_V2_NAMESPACE::try_throw_std_exception_from_error(;  // might throw
      return {std::make_exception_ptr(                                  //
      filelib::filesystem_error(, std::move(fi.path1), std::move(fi.path2),};
}  // namespace convert
View this code on Github

Note that the conversion exactly duplicates the implementation of throw_as_system_error_with_payload(failure_info fi) from namespace filelib. In a production implementation, you probably ought to call that function and catch the exception it throws into a pointer, as that would be more long term maintainable.

Last revised: February 11, 2019 at 13:38:04 UTC

Prev Up HomeNext