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>The Filesystem TS - 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/advanced/payload.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/advanced/payload.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/advanced/payload/copy_file2.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">The Filesystem TS</h1></div></div></div> 14 <p>Something which has long annoyed the purists in the C++ leadership is the problem of dual 15overloads in <code>error_code</code> capable standard library APIs.</p> 16 17<p>Consider the 18<a href="http://en.cppreference.com/w/cpp/filesystem/copy_file"><code>copy_file()</code></a> 19API from the Filesystem TS:</p> 20 21<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">namespace</span> <span class="n">filesystem</span> 22<span class="p">{</span> 23 <span class="cm">/*! Copies the file at path `from` to path `to`. 24</span><span class="cm"> \returns True if file was successfully copied. 25</span><span class="cm"> \throws On failure throws `filesystem_error(ec.message(), from, to, ec)` with 26</span><span class="cm"> `ec` being the error code reported by the operating system. 27</span><span class="cm"> */</span> 28 <span class="kt">bool</span> <span class="n">copy_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span> <span class="o">&</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&</span><span class="n">to</span><span class="p">);</span> 29 30 <span class="cm">/*! Copies the file at path `from` to path `to`. 31</span><span class="cm"> \returns True if file was successfully copied. If false, `ec` is written with 32</span><span class="cm"> the error code reported by the operating system. 33</span><span class="cm"> \throws May throw an exception if there is some "catastrophic" failure 34</span><span class="cm"> e.g. failure to allocate memory. 35</span><span class="cm"> */</span> 36 <span class="kt">bool</span> <span class="nf">copy_file</span><span class="p">(</span><span class="k">const</span> <span class="n">path</span> <span class="o">&</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&</span><span class="n">to</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span> <span class="o">&</span><span class="n">ec</span><span class="p">);</span> 37<span class="p">}</span> 38</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/outcome_payload.cpp#L56" class="code-snippet-url" target="_blank">View this code on Github</a></div> 39 40 41<p>Before Outcome, the common design pattern was to provide throwing and non-throwing overloads 42of every API. As you can see above, the throwing API throws a <a href="http://en.cppreference.com/w/cpp/filesystem/filesystem_error"><code>filesystem::filesystem_error</code></a> 43exception type which carries additional information, specifically two paths. These paths may 44refer to the files which were the source of any failure. However the non-throwing overload 45does <strong>not</strong> provide this additional information, which can make it more annoying to use the 46non-throwing overload sometimes.</p> 47 48<p>What if we could replace these two overloads of every API in the Filesystem TS with a single API, 49and additionally have the non-throwing edition return the exact same additional information 50as the throwing edition?</p> 51 52 53 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 54<hr> 55<div class="spirit-nav"> 56<a accesskey="p" href="../../../tutorial/advanced/payload.html"><img src="../../../images/prev.png" alt="Prev"></a> 57 <a accesskey="u" href="../../../tutorial/advanced/payload.html"><img src="../../../images/up.png" alt="Up"></a> 58 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/payload/copy_file2.html"><img src="../../../images/next.png" alt="Next"></a></div></body> 59</html> 60