12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
- <title>Optional data members</title>
- <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
- <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
- <link rel="home" href="../../index.html" title="Boost.Optional">
- <link rel="up" href="../quick_start.html" title="Quick Start">
- <link rel="prev" href="optional_automatic_variables.html" title="Optional automatic variables">
- <link rel="next" href="bypassing_unnecessary_default_construction.html" title="Bypassing unnecessary default construction">
- </head>
- <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
- <table cellpadding="2" width="100%"><tr>
- <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
- <td align="center"><a href="../../../../../../index.html">Home</a></td>
- <td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
- <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
- <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
- <td align="center"><a href="../../../../../../more/index.htm">More</a></td>
- </tr></table>
- <hr>
- <div class="spirit-nav">
- <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>
- </div>
- <div class="section">
- <div class="titlepage"><div><div><h3 class="title">
- <a name="boost_optional.quick_start.optional_data_members"></a><a class="link" href="optional_data_members.html" title="Optional data members">Optional
- data members</a>
- </h3></div></div></div>
- <p>
- Suppose we want to implement a <span class="emphasis"><em>lazy load</em></span> optimization.
- This is because we do not want to perform an expensive initialization of
- our <code class="computeroutput"><span class="identifier">Resource</span></code> until (if at
- all) it is really used. We can do it this way:
- </p>
- <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Widget</span>
- <span class="special">{</span>
- <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>
- <span class="keyword">public</span><span class="special">:</span>
- <span class="identifier">Widget</span><span class="special">()</span> <span class="special">{}</span>
- <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>
- <span class="special">{</span>
- <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>
- <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>
- <span class="keyword">return</span> <span class="special">*</span><span class="identifier">resource_</span><span class="special">;</span>
- <span class="special">}</span>
- <span class="special">};</span>
- </pre>
- <p>
- <code class="computeroutput"><span class="identifier">optional</span></code>'s default constructor
- creates an uninitialized optional. No call to <code class="computeroutput"><span class="identifier">Resource</span></code>'s
- default constructor is attempted. <code class="computeroutput"><span class="identifier">Resource</span></code>
- 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
- <code class="computeroutput"><span class="identifier">getResource</span></code> we first check
- if <code class="computeroutput"><span class="identifier">resource_</span></code> is initialized.
- This time we do not use the contextual conversion to <code class="computeroutput"><span class="keyword">bool</span></code>,
- but a comparison with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span></code>.
- These two ways are equivalent. Function <code class="computeroutput"><span class="identifier">emplace</span></code>
- initializes the optional in-place by perfect-forwarding the arguments to
- the constructor of <code class="computeroutput"><span class="identifier">Resource</span></code>.
- No copy- or move-construction is involved here. <code class="computeroutput"><span class="identifier">Resource</span></code>
- doesn't even have to be <code class="computeroutput"><span class="identifier">MoveConstructible</span></code>.
- </p>
- <div class="note"><table border="0" summary="Note">
- <tr>
- <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
- <th align="left">Note</th>
- </tr>
- <tr><td align="left" valign="top"><p>
- Function <code class="computeroutput"><span class="identifier">emplace</span></code> is only
- available on compilers that support rvalue references and variadic templates.
- If your compiler does not support these features and you still need to
- avoid any move-constructions, use <a class="link" href="../tutorial/in_place_factories.html" title="In-Place Factories">In-Place
- Factories</a>.
- </p></td></tr>
- </table></div>
- </div>
- <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
- <td align="left"></td>
- <td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2018 Andrzej Krzemieński<p>
- Distributed under the Boost Software License, Version 1.0. (See accompanying
- 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>)
- </p>
- </div></td>
- </tr></table>
- <hr>
- <div class="spirit-nav">
- <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>
- </div>
- </body>
- </html>
|