Boost C++ Libraries

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

Prev Up HomeNext

Using a Result

This models the earlier C++ example of use, and its C equivalent isn’t much more verbose thanks to our helper typedefs and macros:

result positive_only(int x)
{
  if(x < 0)
  {
    return FAILURE(c_enum_bad_argument);
  }
  return SUCCESS(x);
}

bool test(int x)
{
  result r = positive_only(x);
  if(BOOST_OUTCOME_C_RESULT_HAS_ERROR(r))
  {
    if(outcome_status_code_equal_generic(&r.error, EINVAL))
    {
      fprintf(stderr, "Positive numbers only!\n");
      return false;
    }
  }
  return true;
}
View this code on Github

For this to link, the BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM_FROM_ENUM macro needs to be compiled at least once within C++ within the final binary to emit the extern functions needed by C.

Last revised: July 16, 2024 at 21:33:35 +0100


Prev Up HomeNext