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>Using Outcome from C code - 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="../experimental/outcome.html"><img src="../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../experimental.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="../experimental/c-api/limitations.html"><img src="../images/next.png" alt="Next"></a></div><div id="content"> 13 14 <div class="titlepage"><div><div><h1 style="clear: both">Using Outcome from C code</h1></div></div></div> 15<p>A long standing problem for C code (or more usually nowadays, the many other programming 16languages which can speak the C ABI but not the C++ ABI) is how to interpret C++ exception throws. The answer 17is of course that they cannot, thus requiring one to write C shim code on the C++ side 18of things of the form:</p> 19<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// The API we wish to expose to C 20</span><span class="c1"></span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="nf">get_value</span><span class="p">(</span><span class="kt">double</span> <span class="n">v</span><span class="p">);</span> 21 22<span class="c1">// The C shim function for the C++ get_value() function. 23</span><span class="c1"></span><span class="k">extern</span> <span class="s">"C"</span> <span class="kt">int</span> <span class="n">c_get_value</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">**</span><span class="n">ret</span><span class="p">,</span> <span class="kt">double</span> <span class="n">v</span><span class="p">)</span> 24<span class="p">{</span> 25 <span class="k">try</span> 26 <span class="p">{</span> 27 <span class="o">*</span><span class="n">ret</span> <span class="o">=</span> <span class="n">get_value</span><span class="p">(</span><span class="n">v</span><span class="p">);</span> 28 <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// success 29</span><span class="c1"></span> <span class="p">}</span> 30 <span class="k">catch</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">range_error</span> <span class="o">&</span><span class="p">)</span> 31 <span class="p">{</span> 32 <span class="k">return</span> <span class="n">ERANGE</span><span class="p">;</span> 33 <span class="p">}</span> 34 <span class="c1">// More catch clauses may go in here ... 35</span><span class="c1"></span> <span class="k">catch</span><span class="p">(...)</span> 36 <span class="p">{</span> 37 <span class="k">return</span> <span class="n">EAGAIN</span><span class="p">;</span> 38 <span class="p">}</span> 39<span class="p">}</span> 40</code></pre></div> 41<p>This is sufficiently painful that most reach for a bindings generator tool like 42<a href="http://www.swig.org/">SWIG</a> to automate this sort of tedious boilerplate generation. 43And this is fine for larger projects, but for smaller projects the cost of 44setting up and configuring SWIG is also non-trivial.</p> 45 46<p>What would be really great is if <code>result<T></code> returning <code>noexcept</code> C++ functions 47could be used straight from C. And indeed Experimental Outcome provides just that facility 48which this section covers next.</p> 49 50 51 52 </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p> 53<hr> 54<div class="spirit-nav"> 55<a accesskey="p" href="../experimental/outcome.html"><img src="../images/prev.png" alt="Prev"></a> 56 <a accesskey="u" href="../experimental.html"><img src="../images/up.png" alt="Up"></a> 57 <a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../experimental/c-api/limitations.html"><img src="../images/next.png" alt="Next"></a></div></body> 58</html> 59