two-phase-init.html 3.8 KB

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