2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-02-01 08:42:13 +00:00
Files
outcome/tutorial/constructors/two-phase-init.html
2019-01-10 22:37:01 +00:00

79 lines
4.6 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>Two phase construction - 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/constructors.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/constructors.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/constructors/file_handle.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h1 style="clear: both">Two phase construction</h1></div></div></div>
<p>The first thing to do is to break your object&rsquo;s construction into two phases:</p>
<ol>
<li><p>Place the object into a state where it can be legally destructed
without doing any initialisation which could throw an exception (i.e. everything
done in phase 1 is <code>constexpr</code>). This phase usually involves initialising member
variables to various default values, most often using default member initialisers.
Most standard C++ library objects
and containers have <code>constexpr</code> constructors, and thus can be initialised
during phase 1. If you need to initialise a member variable without
a <code>constexpr</code> constructor, <code>std::optional&lt;T&gt;</code> is the usual workaround.</p></li>
<li><p>Do the remainder of the construction, the parts which could fail.
Because phase 1 placed the object into a legally destructible state,
it is usually the case that one can bail out during phase 2 and the
destructor will clean things up correctly.</p></li>
</ol>
<p>The phase 1 construction will be placed into a <em>private</em> <code>constexpr</code>
constructor so nobody external can call it. The phase 2 construction will be placed into a static
member initialisation function which returns a <code>result</code> with either
the successfully constructed object, or the cause of any failure to
construct the object.</p>
<p>Finally, as a phase 3,
some simple metaprogramming will implement a <code>construct&lt;T&gt;{Args...}()</code>
free function which will construct any object <code>T</code> by calling its
static initialisation function with <code>Args...</code> and returning the
<code>result</code> returned. This isn&rsquo;t as nice as calling <code>T(Args...)</code> directly,
but it&rsquo;s not too bad in practice. And more importantly, it lets you
write generic code which can construct any unknown object which
fails via returning <code>result</code>, completely avoiding C++ exception throws.</p>
</div><p><small>Last revised: December 12, 2017 at 10:02:44 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/constructors.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/constructors.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/constructors/file_handle.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>