]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Bypassing unnecessary default construction</title> | |
5 | <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css"> | |
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.78.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_data_members.html" title="Optional data members"> | |
10 | <link rel="next" href="storage_in_containers.html" title="Storage in containers"> | |
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_data_members.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="storage_in_containers.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.bypassing_unnecessary_default_construction"></a><a class="link" href="bypassing_unnecessary_default_construction.html" title="Bypassing unnecessary default construction">Bypassing | |
28 | unnecessary default construction</a> | |
29 | </h3></div></div></div> | |
30 | <p> | |
31 | Suppose we have class <code class="computeroutput"><span class="identifier">Date</span></code>, | |
32 | which does not have a default constructor: there is no good candidate for | |
33 | a default date. We have a function that returns two dates in form of a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">tuple</span></code>: | |
34 | </p> | |
35 | <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">tuple</span><span class="special"><</span><span class="identifier">Date</span><span class="special">,</span> <span class="identifier">Date</span><span class="special">></span> <span class="identifier">getPeriod</span><span class="special">();</span> | |
36 | </pre> | |
37 | <p> | |
38 | In other place we want to use the result of <code class="computeroutput"><span class="identifier">getPeriod</span></code>, | |
39 | but want the two dates to be named: <code class="computeroutput"><span class="identifier">begin</span></code> | |
40 | and <code class="computeroutput"><span class="identifier">end</span></code>. We want to implement | |
41 | something like 'multiple return values': | |
42 | </p> | |
43 | <pre class="programlisting"><span class="identifier">Date</span> <span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> <span class="comment">// Error: no default ctor!</span> | |
44 | <span class="identifier">boost</span><span class="special">::</span><span class="identifier">tie</span><span class="special">(</span><span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">getPeriod</span><span class="special">();</span> | |
45 | </pre> | |
46 | <p> | |
47 | The second line works already, this is the capability of <a href="../../../../../tuple/index.html" target="_top">Boost.Tuple</a> | |
48 | library, but the first line won't work. We could set some invented initial | |
49 | dates, but it is confusing and may be an unacceptable cost, given that these | |
50 | values will be overwritten in the next line anyway. This is where <code class="computeroutput"><span class="identifier">optional</span></code> can help: | |
51 | </p> | |
52 | <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="identifier">Date</span><span class="special">></span> <span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">;</span> | |
53 | <span class="identifier">boost</span><span class="special">::</span><span class="identifier">tie</span><span class="special">(</span><span class="identifier">begin</span><span class="special">,</span> <span class="identifier">end</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">getPeriod</span><span class="special">();</span> | |
54 | </pre> | |
55 | <p> | |
56 | It works because inside <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">tie</span></code> a | |
57 | move-assignment from <code class="computeroutput"><span class="identifier">T</span></code> is | |
58 | invoked on <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>, | |
59 | which internally calls a move-constructor of <code class="computeroutput"><span class="identifier">T</span></code>. | |
60 | </p> | |
61 | </div> | |
62 | <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> | |
63 | <td align="left"></td> | |
64 | <td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2016 Andrzej Krzemieński<p> | |
65 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
66 | 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>) | |
67 | </p> | |
68 | </div></td> | |
69 | </tr></table> | |
70 | <hr> | |
71 | <div class="spirit-nav"> | |
72 | <a accesskey="p" href="optional_data_members.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="storage_in_containers.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
73 | </div> | |
74 | </body> | |
75 | </html> |