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>Coroutine TRY operation - 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/coroutines.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/essential/coroutines.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/coroutines/awaitables.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">Coroutine TRY operation</h1></div></div></div> 14 <p>As one cannot call statement <code>return</code> from within a Coroutine, the very first part of Outcome’s 15support for Coroutines is <a href="../../../reference/macros/co_tryv.html" class="api-reference"><code>BOOST_OUTCOME_CO_TRYV(expr)/BOOST_OUTCOME_CO_TRY(expr)</code></a> 16, 17which is literally the same as <code>BOOST_OUTCOME_TRY()</code> except that <code>co_return</code> is called 18to return early instead of <code>return</code>.</p> 19<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">eager</span><span class="o"><</span><span class="n">result</span><span class="o"><</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">>></span> <span class="n">to_string</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">)</span> 20<span class="p">{</span> 21 <span class="k">if</span><span class="p">(</span><span class="n">x</span> <span class="o">>=</span> <span class="mi">0</span><span class="p">)</span> 22 <span class="p">{</span> 23 <span class="n">BOOST_OUTCOME_CO_TRY</span><span class="p">(</span><span class="n">convert</span><span class="p">(</span><span class="n">x</span><span class="p">));</span> 24 <span class="p">}</span> 25 <span class="n">co_return</span> <span class="s">"out of range"</span><span class="p">;</span> 26<span class="p">}</span> 27</code></pre></div> 28 29 </div><p><small>Last revised: April 07, 2020 at 10:22:38 +0100</small></p> 30<hr> 31<div class="spirit-nav"> 32<a accesskey="p" href="../../../tutorial/essential/coroutines.html"><img src="../../../images/prev.png" alt="Prev"></a> 33 <a accesskey="u" href="../../../tutorial/essential/coroutines.html"><img src="../../../images/up.png" alt="Up"></a> 34 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/coroutines/awaitables.html"><img src="../../../images/next.png" alt="Next"></a></div></body> 35</html> 36