1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 3<title>TRY operations - Boost.Outcome documentation</title> 4<link rel="stylesheet" href="../../../css/boost.css" type="text/css"> 5<meta name="generator" content="Hugo 0.52 with Boostdoc theme"> 6<meta name="viewport" content="width=device-width,initial-scale=1.0"/> 7 8<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/> 9<body><div class="spirit-nav"> 10<a accesskey="p" href="../../../tutorial/essential/result/inspecting.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a> 12 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">TRY operations</h1></div></div></div> 14 15 16<p>In the implementation of function <code>print_half</code> we have seen the usage of the macro <a href="../../../reference/macros/tryv.html" class="api-reference"><code>BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr)</code></a> 17:</p> 18<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">BOOST_OUTCOME_TRY</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">));</span> 19</code></pre></div> 20<p>The <code>BOOST_OUTCOME_TRY</code> macro uses C macro overloading to select between two implementations based on the number of 21input parameters. If there is exactly one input parameter i.e. without the <code>i</code>, the control statement is 22roughly equivalent to:</p> 23<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span><span class="o">&&</span> <span class="n">__result</span> <span class="o">=</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">);</span> 24<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">__result</span><span class="p">)</span> 25 <span class="k">return</span> <span class="n">__result</span><span class="p">.</span><span class="n">as_failure</span><span class="p">();</span> 26</code></pre></div> 27<p>Where <code>__result</code> is a compile time generated unique name. This single argument form is equivalent to 28<code>BOOST_OUTCOME_TRYV(expr)</code>, incidentally.</p> 29 30<p>If there are between two and eight parameters, this control statement is roughly equivalent to:</p> 31<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span><span class="o">&&</span> <span class="n">__result</span> <span class="o">=</span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">);</span> 32<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">__result</span><span class="p">)</span> 33 <span class="k">return</span> <span class="n">__result</span><span class="p">.</span><span class="n">as_failure</span><span class="p">();</span> 34<span class="k">auto</span><span class="o">&&</span> <span class="n">i</span> <span class="o">=</span> <span class="n">__result</span><span class="p">.</span><span class="n">value</span><span class="p">();</span> 35</code></pre></div> 36<p>So here <code>i</code> as the first C macro parameter is set to the value of any successful result.</p> 37 38<p>C macro overloads are provided for up to eight arguments. To prevent the 39confounding of the C preprocessor by commas in template specifications causing more than 40eight arguments appearing to the C preprocessor, you should consider wrapping the 41second argument in brackets.</p> 42 43<hr> 44 45<h3 id="compiler-specific-extension-boost-outcome-tryx">Compiler-specific extension: <code>BOOST_OUTCOME_TRYX</code></h3> 46 47<div class="notices note" style="background: url('../../../images/note.png') top left no-repeat padding-box padding-box;"> 48<div class="notices heading">note</div> 49<div class="notices message"><p>This macro makes use of a proprietary extension in GCC and clang, and is not 50portable. The macro is not made available on unsupported compilers, 51so you can test for its presence using <code>#ifdef BOOST_OUTCOME_TRYX</code>.</p> 52</div> 53</div> 54 55 56<p>GCC and Clang provide an extension to C++ known as 57<a href="https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html" title="GCC docs on statement expressions">statement expressions</a>. 58These make it possible to use a more convenient macro: <code>BOOST_OUTCOME_TRYX</code>, which is an expression. With the above macro, the above declaration of variable <code>i</code> can be rewritten to:</p> 59<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span> <span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">));</span> 60</code></pre></div> 61<p>This has an advantage that you can use it any place where you can put an expression, e.g., in “simple initialization”:</p> 62<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">if</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">)))</span> 63 <span class="n">use_a_non_zero_int</span><span class="p">(</span><span class="n">i</span><span class="p">);</span> 64</code></pre></div> 65<p>or in as a subexpression of a bigger full expression:</p> 66<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="n">ans</span> <span class="o">=</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="s">"1"</span><span class="p">))</span> <span class="o">+</span> <span class="n">BOOST_OUTCOME_TRYX</span><span class="p">(</span><span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="s">"2"</span><span class="p">));</span> 67</code></pre></div> 68 69 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 70<hr> 71<div class="spirit-nav"> 72<a accesskey="p" href="../../../tutorial/essential/result/inspecting.html"><img src="../../../images/prev.png" alt="Prev"></a> 73 <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a> 74 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_greedy.html"><img src="../../../images/next.png" alt="Next"></a></div></body> 75</html> 76