TRY a C Result

Thanks to much of the magic of 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)
{
  CXX_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.