]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
3 | <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> | |
4 | <title>Optional automatic variables</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="../quick_start.html" title="Quick Start"> | |
10 | <link rel="next" href="optional_data_members.html" title="Optional data members"> | |
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="../quick_start.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="optional_data_members.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_automatic_variables"></a><a class="link" href="optional_automatic_variables.html" title="Optional automatic variables">Optional | |
28 | automatic variables</a> | |
29 | </h3></div></div></div> | |
30 | <p> | |
31 | We could write function <code class="computeroutput"><span class="identifier">convert</span></code> | |
32 | in a slightly different manner, so that it has a single <code class="computeroutput"><span class="keyword">return</span></code>-statement: | |
33 | </p> | |
34 | <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">convert</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&</span> <span class="identifier">text</span><span class="special">)</span> | |
35 | <span class="special">{</span> | |
36 | <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">ans</span><span class="special">;</span> | |
37 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">stringstream</span> <span class="identifier">s</span><span class="special">(</span><span class="identifier">text</span><span class="special">);</span> | |
38 | <span class="keyword">int</span> <span class="identifier">i</span><span class="special">;</span> | |
39 | <span class="keyword">if</span> <span class="special">((</span><span class="identifier">s</span> <span class="special">>></span> <span class="identifier">i</span><span class="special">)</span> <span class="special">&&</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special">==</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">char_traits</span><span class="special"><</span><span class="keyword">char</span><span class="special">>::</span><span class="identifier">eof</span><span class="special">())</span> | |
40 | <span class="identifier">ans</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> | |
41 | ||
42 | <span class="keyword">return</span> <span class="identifier">ans</span><span class="special">;</span> | |
43 | <span class="special">}</span> | |
44 | </pre> | |
45 | <p> | |
46 | The default constructor of <code class="computeroutput"><span class="identifier">optional</span></code> | |
47 | creates an unitialized optional object. Unlike with <code class="computeroutput"><span class="keyword">int</span></code>s | |
48 | you cannot have an <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span></code> | |
49 | in an indeterminate state. Its state is always well defined. Instruction | |
50 | <code class="computeroutput"><span class="identifier">ans</span> <span class="special">=</span> | |
51 | <span class="identifier">i</span></code> initializes the optional object. | |
52 | It uses the 'mixed' assignment from <code class="computeroutput"><span class="keyword">int</span></code>. | |
53 | In general, for <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>, | |
54 | when an assignment from <code class="computeroutput"><span class="identifier">T</span></code> | |
55 | is invoked, it can do two things. If the optional object is not initialized | |
56 | (our case here), it initializes the contained value using <code class="computeroutput"><span class="identifier">T</span></code>'s | |
57 | copy constructor. If the optional object is already initialized, it assigns | |
58 | the new value to it using <code class="computeroutput"><span class="identifier">T</span></code>'s | |
59 | copy assignment. | |
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="../quick_start.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="optional_data_members.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> | |
73 | </div> | |
74 | </body> | |
75 | </html> |