2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-01-19 04:22:13 +00:00
Files
outcome/doc/html/experimental/c-api/from-c/try.html
2025-12-16 21:35:16 +00:00

44 lines
3.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TRY a C Result - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../experimental/c-api/from-c/use.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../experimental/c-api/from-c.html"><img src="../../../images/up.png" alt="Up"></a>
<a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../reference.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h1 style="clear: both">TRY a C Result</h1></div></div></div>
<p>Thanks to much of the magic of <a href="../../../reference/macros/try.html" class="api-reference"><code>BOOST_OUTCOME_TRY(var, expr)</code></a>
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:</p>
<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">result</span> <span class="nf">test2</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">BOOST_OUTCOME_C_RESULT_SYSTEM_TRY</span><span class="p">(</span><span class="kt">int</span> <span class="n">v</span><span class="p">,</span> <span class="c1">// what to set to value if successful
</span><span class="c1"></span> <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#34;Positive numbers only!</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">),</span> <span class="c1">// what cleanup to run if unsuccessful
</span><span class="c1"></span> <span class="n">positive_only</span><span class="p">(</span><span class="n">x</span><span class="p">));</span>
<span class="k">return</span> <span class="n">SUCCESS</span><span class="p">(</span><span class="n">v</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/c_api2.cpp#L91" class="code-snippet-url" target="_blank">View this code on Github</a></div>
<p>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.</p>
<p>Also due to lack of type sugaring and user defined implicit conversions, if your
callers result type isn&rsquo;t your callee&rsquo;s, you may need to specify what your caller&rsquo;s
result type is so the error state can be correctly propagated.</p>
</div><p><small>Last revised: July 16, 2024 at 21:33:35 &#43;0100</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../experimental/c-api/from-c/use.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../experimental/c-api/from-c.html"><img src="../../../images/up.png" alt="Up"></a>
<a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../reference.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>