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>Two phase construction - 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/constructors.html"><img src="../../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../../tutorial/advanced/constructors.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/constructors/file_handle.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">Two phase construction</h1></div></div></div> 14 <p>The first thing to do is to break your object’s construction into two phases:</p> 15 16<ol> 17<li><p>Place the object into a state where it can be legally destructed 18without doing any initialisation which could throw an exception (i.e. everything 19done in phase 1 is <code>constexpr</code>). This phase usually involves initialising member 20variables to various default values, most often using default member initialisers. 21Most standard C++ library objects 22and containers have <code>constexpr</code> constructors, and thus can be initialised 23during phase 1. If you need to initialise a member variable without 24a <code>constexpr</code> constructor, 25<a href="https://en.cppreference.com/w/cpp/utility/optional" class="api-reference" target="_blank"><i class="fa fa-book" aria-hidden="true"></i> <code>std::optional<T></code></a> 26 is the usual workaround.</p></li> 27 28<li><p>Do the remainder of the construction, the parts which could fail. 29Because phase 1 placed the object into a legally destructible state, 30it is usually the case that one can bail out during phase 2 and the 31destructor will clean things up correctly.</p></li> 32</ol> 33 34<p>The phase 1 construction will be placed into a <em>private</em> <code>constexpr</code> 35constructor so nobody external can call it. The phase 2 construction will be placed into a static 36member initialisation function which returns a <code>result</code> with either 37the successfully constructed object, or the cause of any failure to 38construct the object.</p> 39 40<p>Finally, as a phase 3, 41some simple metaprogramming will implement a <code>make<T>{Args...}()</code> 42free function which will construct any object <code>T</code> by calling its 43static initialisation function with <code>Args...</code> and returning the 44<code>result</code> returned. This isn’t as nice as calling <code>T(Args...)</code> directly, 45but it’s not too bad in practice. And more importantly, it lets you 46write generic code which can construct any unknown object which 47fails via returning <code>result</code>, completely avoiding C++ exception throws.</p> 48 49 50 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 51<hr> 52<div class="spirit-nav"> 53<a accesskey="p" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/prev.png" alt="Prev"></a> 54 <a accesskey="u" href="../../../tutorial/advanced/constructors.html"><img src="../../../images/up.png" alt="Up"></a> 55 <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/constructors/file_handle.html"><img src="../../../images/next.png" alt="Next"></a></div></body> 56</html> 57