2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-02-21 15:22:09 +00:00
Files
outcome/tutorial/payload/copy_file.html
2018-12-07 15:37:11 +00:00

83 lines
5.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>The Filesystem TS - 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 id="boost-common-heading-doc" style="background: #574D74 url('../../images/header-bg.png') repeat-x top left;">
<div class="heading-inner" style="background: url('../../images/header-fg.png') no-repeat top left;">
<div class="heading-placard"></div>
<h1 class="heading-title">
<a href="../../">
<img src="../../images/space.png" alt="Boost C++ Libraries" class="heading-logo" />
<span class="heading-boost">Boost</span>
<span class="heading-cpplibraries">C++ Libraries</span>
</a>
</h1>
<p class="heading-quote">
<q>...one of the most highly
regarded and expertly designed C++ library projects in the
world.</q> <span class="heading-attribution">&mdash; <a href=
"http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href=
"http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei
Alexandrescu</a>, <a href=
"http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++
Coding Standards</a></span></p>
</div>
</div>
<div id="boost-common-heading-doc-spacer"></div>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/payload.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/payload.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="../../tutorial/payload/copy_file2.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">The Filesystem TS</h2></div></div></div>
<p>Something which has long annoyed the purists in the C++ leadership is the problem of dual
overloads in <code>error_code</code> capable standard library APIs.</p>
<p>Consider the
<a href="http://en.cppreference.com/w/cpp/filesystem/copy_file"><code>copy_file()</code></a>
API from the Filesystem TS:</p>
<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>
<span class="p">{</span>
<span class="cm">/*! Copies the file at path `from` to path `to`.
</span><span class="cm"> \returns True if file was successfully copied.
</span><span class="cm"> \throws On failure throws `filesystem_error(ec.message(), from, to, ec)` with
</span><span class="cm"> `ec` being the error code reported by the operating system.
</span><span class="cm"> */</span>
<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">&amp;</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</span><span class="n">to</span><span class="p">);</span>
<span class="cm">/*! Copies the file at path `from` to path `to`.
</span><span class="cm"> \returns True if file was successfully copied. If false, `ec` is written with
</span><span class="cm"> the error code reported by the operating system.
</span><span class="cm"> \throws Never throws.
</span><span class="cm"> */</span>
<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">&amp;</span><span class="n">from</span><span class="p">,</span> <span class="k">const</span> <span class="n">path</span> <span class="o">&amp;</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">&amp;</span><span class="n">ec</span><span class="p">)</span> <span class="k">noexcept</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/outcome_payload.cpp#L19" class="code-snippet-url" target="_blank">View this code on Github</a></div>
<p>Before Outcome, the common design pattern was to provide throwing and non-throwing overloads
of 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>
exception type which carries additional information, specifically two paths. These paths may
refer to the files which were the source of any failure. However the non-throwing overload
does <strong>not</strong> provide this additional information, which can make it more annoying to use the
non-throwing overload sometimes.</p>
<p>What if we could replace these two overloads of every API in the Filesystem TS with a single API,
and additionally have the non-throwing edition return the exact same additional information
as the throwing edition?</p>
</div><p><small>Last revised: December 05, 2018 at 14:18:52 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/payload.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/payload.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="../../tutorial/payload/copy_file2.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>