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

TRY a C Result

Thanks to much of the magic of BOOST_OUTCOME_TRY(var, expr) being implemented using C preprocessor metaprogramming, we can offer a very similar experience for the C try operation and without needing anything compiled in C++ as support functions:

result test2(int x)
{
  BOOST_OUTCOME_C_RESULT_SYSTEM_TRY(int v,                                        // what to set to value if successful
                        fprintf(stderr, "Positive numbers only!\n"),  // what cleanup to run if unsuccessful
                        positive_only(x));
  return SUCCESS(v + 1);
}
View this code on Github

The principle difference is that you can specify a cleanup routine to perform if failure is encountered. This is especially useful in C, which has no stack unwinding.

Also due to lack of type sugaring and user defined implicit conversions, if your callers result type isn’t your callee’s, you may need to specify what your caller’s result type is so the error state can be correctly propagated.

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


Prev Up HomeNext