1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Optional data members</title> 5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Boost.Optional"> 8<link rel="up" href="../quick_start.html" title="Quick Start"> 9<link rel="prev" href="optional_automatic_variables.html" title="Optional automatic variables"> 10<link rel="next" href="bypassing_unnecessary_default_construction.html" title="Bypassing unnecessary default construction"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="optional_automatic_variables.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quick_start.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bypassing_unnecessary_default_construction.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_optional.quick_start.optional_data_members"></a><a class="link" href="optional_data_members.html" title="Optional data members">Optional 28 data members</a> 29</h3></div></div></div> 30<p> 31 Suppose we want to implement a <span class="emphasis"><em>lazy load</em></span> optimization. 32 This is because we do not want to perform an expensive initialization of 33 our <code class="computeroutput"><span class="identifier">Resource</span></code> until (if at 34 all) it is really used. We can do it this way: 35 </p> 36<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Widget</span> 37<span class="special">{</span> 38 <span class="keyword">mutable</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">const</span> <span class="identifier">Resource</span><span class="special">></span> <span class="identifier">resource_</span><span class="special">;</span> 39 40<span class="keyword">public</span><span class="special">:</span> 41 <span class="identifier">Widget</span><span class="special">()</span> <span class="special">{}</span> 42 43 <span class="keyword">const</span> <span class="identifier">Resource</span><span class="special">&</span> <span class="identifier">getResource</span><span class="special">()</span> <span class="keyword">const</span> <span class="comment">// not thread-safe</span> 44 <span class="special">{</span> 45 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">resource_</span> <span class="special">==</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span><span class="special">)</span> 46 <span class="identifier">resource_</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="string">"resource"</span><span class="special">,</span> <span class="string">"arguments"</span><span class="special">);</span> 47 48 <span class="keyword">return</span> <span class="special">*</span><span class="identifier">resource_</span><span class="special">;</span> 49 <span class="special">}</span> 50<span class="special">};</span> 51</pre> 52<p> 53 <code class="computeroutput"><span class="identifier">optional</span></code>'s default constructor 54 creates an uninitialized optional. No call to <code class="computeroutput"><span class="identifier">Resource</span></code>'s 55 default constructor is attempted. <code class="computeroutput"><span class="identifier">Resource</span></code> 56 doesn't have to be <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">DefaultConstructible</span></code></a>. In function 57 <code class="computeroutput"><span class="identifier">getResource</span></code> we first check 58 if <code class="computeroutput"><span class="identifier">resource_</span></code> is initialized. 59 This time we do not use the contextual conversion to <code class="computeroutput"><span class="keyword">bool</span></code>, 60 but a comparison with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span></code>. 61 These two ways are equivalent. Function <code class="computeroutput"><span class="identifier">emplace</span></code> 62 initializes the optional in-place by perfect-forwarding the arguments to 63 the constructor of <code class="computeroutput"><span class="identifier">Resource</span></code>. 64 No copy- or move-construction is involved here. <code class="computeroutput"><span class="identifier">Resource</span></code> 65 doesn't even have to be <code class="computeroutput"><span class="identifier">MoveConstructible</span></code>. 66 </p> 67<div class="note"><table border="0" summary="Note"> 68<tr> 69<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td> 70<th align="left">Note</th> 71</tr> 72<tr><td align="left" valign="top"><p> 73 Function <code class="computeroutput"><span class="identifier">emplace</span></code> is only 74 available on compilers that support rvalue references and variadic templates. 75 If your compiler does not support these features and you still need to 76 avoid any move-constructions, use <a class="link" href="../tutorial/in_place_factories.html" title="In-Place Factories">In-Place 77 Factories</a>. 78 </p></td></tr> 79</table></div> 80</div> 81<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 82<td align="left"></td> 83<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2018 Andrzej Krzemieński<p> 84 Distributed under the Boost Software License, Version 1.0. (See accompanying 85 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 86 </p> 87</div></td> 88</tr></table> 89<hr> 90<div class="spirit-nav"> 91<a accesskey="p" href="optional_automatic_variables.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quick_start.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="bypassing_unnecessary_default_construction.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 92</div> 93</body> 94</html> 95