Hybrid result/try/catch vs a new direction in C++

Also for reference, see Boost Outcome, which has been accepted into Boost as of 1.69. It works on C++14 without any langauge changes. It's basically as close to Rust's Result as is possible without language level changes.

auto get_int_from_file(string_view path) noexcept
-> outcome::result<int>
{
  OUTCOME_TRY(str, (read_data_from_file(path)));
  // if control gets here read_data_from_file() has succeeded
  return process(str);  // decltype(str) == string
}

I was initially excited about the prospect of using Rust-style return-oriented error handling, but my enthusiasm has been dampened as I've dug more into it, to the point where I am sad to report I'll probably stick to exceptions for my typical "modern" C++ projects. :slightly_frowning_face:

What Outcome doesn't have:

  • The convenient, short, ? operator
  • match
  • Compile time guarantees you've upwrapped things correctly

You will probably find this surprising. This is because the default action for a user-defined error type is undefined behaviour

I really don't have space in my brain for one more possible way to add UB to my C++ projects.

All of this lead me to believe Rust did a really fabulous job on how it does error handling, you really need all three of these features to make return-oriented error handling worth the overhead over the conveniences of happy-path-optimized exception handling.

5 Likes