• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Gotchas</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="../../optional/tutorial.html" title="Tutorial">
9<link rel="prev" href="in_place_factories.html" title="In-Place Factories">
10<link rel="next" href="gotchas/moved_from__optional_.html" title="Moved-from optional">
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="in_place_factories.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.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="gotchas/moved_from__optional_.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.tutorial.gotchas"></a><a class="link" href="gotchas.html" title="Gotchas">Gotchas</a>
28</h3></div></div></div>
29<div class="section">
30<div class="titlepage"><div><div><h4 class="title">
31<a name="boost_optional.tutorial.gotchas.a_note_about_optional_bool_"></a><a class="link" href="gotchas.html#boost_optional.tutorial.gotchas.a_note_about_optional_bool_" title="A note about optional&lt;bool&gt;">A
32        note about optional&lt;bool&gt;</a>
33</h4></div></div></div>
34<p>
35          <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code>
36          should be used with special caution and consideration.
37        </p>
38<p>
39          First, it is functionally similar to a tristate boolean (false, maybe,
40          true) —such as <a href="../../../../../../doc/html/tribool.html" target="_top">boost::tribool</a>—
41          except that in a tristate boolean, the maybe state <span class="underline">represents
42          a valid value</span>, unlike the corresponding state of an uninitialized
43          <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code>.
44          It should be carefully considered if an <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code> instead of a <code class="computeroutput"><span class="identifier">tribool</span></code>
45          is really needed.
46        </p>
47<p>
48          Second, although <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;&gt;</span></code> provides a contextual conversion
49          to <code class="computeroutput"><span class="keyword">bool</span></code> in C++11, this falls
50          back to an implicit conversion on older compilers. This conversion refers
51          to the initialization state and not to the contained value. Using <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span></code>
52          can lead to subtle errors due to the implicit <code class="computeroutput"><span class="keyword">bool</span></code>
53          conversion:
54        </p>
55<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">foo</span> <span class="special">(</span> <span class="keyword">bool</span> <span class="identifier">v</span> <span class="special">)</span> <span class="special">;</span>
56<span class="keyword">void</span> <span class="identifier">bar</span><span class="special">()</span>
57<span class="special">{</span>
58    <span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">v</span> <span class="special">=</span> <span class="keyword">try</span><span class="special">();</span>
59
60    <span class="comment">// The following intended to pass the value of 'v' to foo():</span>
61    <span class="identifier">foo</span><span class="special">(</span><span class="identifier">v</span><span class="special">);</span>
62    <span class="comment">// But instead, the initialization state is passed</span>
63    <span class="comment">// due to a typo: it should have been foo(*v).</span>
64<span class="special">}</span>
65</pre>
66<p>
67          The only implicit conversion is to <code class="computeroutput"><span class="keyword">bool</span></code>,
68          and it is safe in the sense that typical integral promotions don't apply
69          (i.e. if <code class="computeroutput"><span class="identifier">foo</span><span class="special">()</span></code>
70          takes an <code class="computeroutput"><span class="keyword">int</span></code> instead, it won't
71          compile).
72        </p>
73<p>
74          Third, mixed comparisons with <code class="computeroutput"><span class="keyword">bool</span></code>
75          work differently than similar mixed comparisons between pointers and <code class="computeroutput"><span class="keyword">bool</span></code>, so the results might surprise you:
76        </p>
77<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="identifier">oEmpty</span><span class="special">(</span><span class="identifier">none</span><span class="special">),</span> <span class="identifier">oTrue</span><span class="special">(</span><span class="keyword">true</span><span class="special">),</span> <span class="identifier">oFalse</span><span class="special">(</span><span class="keyword">false</span><span class="special">);</span>
78
79<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oEmpty</span> <span class="special">==</span> <span class="identifier">none</span><span class="special">);</span>  <span class="comment">// renders true</span>
80<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oEmpty</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">);</span> <span class="comment">// renders false!</span>
81<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oEmpty</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">);</span>  <span class="comment">// renders false!</span>
82
83<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oFalse</span> <span class="special">==</span> <span class="identifier">none</span><span class="special">);</span>  <span class="comment">// renders false</span>
84<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oFalse</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">);</span> <span class="comment">// renders true!</span>
85<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oFalse</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">);</span>  <span class="comment">// renders false</span>
86
87<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oTrue</span> <span class="special">==</span> <span class="identifier">none</span><span class="special">);</span>   <span class="comment">// renders false</span>
88<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oTrue</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">);</span>  <span class="comment">// renders false</span>
89<span class="keyword">if</span> <span class="special">(</span><span class="identifier">oTrue</span> <span class="special">==</span> <span class="keyword">true</span><span class="special">);</span>   <span class="comment">// renders true</span>
90</pre>
91<p>
92          In other words, for <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;&gt;</span></code>, the following assertion does not
93          hold:
94        </p>
95<pre class="programlisting"><span class="identifier">assert</span><span class="special">((</span><span class="identifier">opt</span> <span class="special">==</span> <span class="keyword">false</span><span class="special">)</span> <span class="special">==</span> <span class="special">(!</span><span class="identifier">opt</span><span class="special">));</span>
96</pre>
97</div>
98</div>
99<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
100<td align="left"></td>
101<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2018 Andrzej Krzemieński<p>
102        Distributed under the Boost Software License, Version 1.0. (See accompanying
103        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>)
104      </p>
105</div></td>
106</tr></table>
107<hr>
108<div class="spirit-nav">
109<a accesskey="p" href="in_place_factories.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.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="gotchas/moved_from__optional_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
110</div>
111</body>
112</html>
113