• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5<title>Tutorial</title>
6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
9<link rel="up" href="../tribool.html" title="Chapter 23. Boost.Tribool">
10<link rel="prev" href="../tribool.html" title="Chapter 23. Boost.Tribool">
11<link rel="next" href="reference.html" title="Reference">
12</head>
13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
14<table cellpadding="2" width="100%"><tr>
15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
16<td align="center"><a href="../../../index.html">Home</a></td>
17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
20<td align="center"><a href="../../../more/index.htm">More</a></td>
21</tr></table>
22<hr>
23<div class="spirit-nav">
24<a accesskey="p" href="../tribool.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tribool.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
25</div>
26<div class="section">
27<div class="titlepage"><div><div><h2 class="title" style="clear: both">
28<a name="tribool.tutorial"></a>Tutorial</h2></div></div></div>
29<div class="toc"><dl class="toc">
30<dt><span class="section"><a href="tutorial.html#id-1.3.24.4.2">Basic usage</a></span></dt>
31<dt><span class="section"><a href="tutorial.html#id-1.3.24.4.3">Renaming the indeterminate state</a></span></dt>
32<dt><span class="section"><a href="tutorial.html#id-1.3.24.4.4"><code class="computeroutput">tribool</code> input/output</a></span></dt>
33</dl></div>
34<div class="section">
35<div class="titlepage"><div><div><h3 class="title">
36<a name="id-1.3.24.4.2"></a>Basic usage</h3></div></div></div>
37<p> The <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> class acts
38    like the built-in <code class="computeroutput">bool</code> type, but for 3-state boolean
39    logic. The three states are <code class="computeroutput">true</code>, <code class="computeroutput">false</code>,
40    and <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>, where
41    the first two states are equivalent to those of the C++
42    <code class="computeroutput">bool</code> type and the last state represents an unknown
43    boolean value (that may be <code class="computeroutput">true</code> or
44    <code class="computeroutput">false</code>, we don't know).</p>
45<p> The <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> class
46    supports conversion from <code class="computeroutput">bool</code> values and literals
47    along with its own
48    <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>
49    keyword:</p>
50<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> b(true);
51b = false;
52b = <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>;
53<code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> b2(b);</pre>
54<p> <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> supports
55    conversions to <code class="computeroutput">bool</code> for use in conditional
56    statements. The conversion to <code class="computeroutput">bool</code> will be
57    <code class="computeroutput">true</code> when the value of the
58    <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> is always true, and
59    <code class="computeroutput">false</code> otherwise. Consequently, the following idiom
60    may be used to determine which of the three states a
61    <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> currently
62    holds:</p>
63<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> b = some_operation();
64if (b) {
65  // b is true
66}
67else if (!b) {
68  // b is false
69}
70else {
71  // b is indeterminate
72}</pre>
73<p> <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> supports the
74  3-state logic operators <code class="computeroutput">!</code> (negation),
75  <code class="computeroutput">&amp;&amp;</code> (AND), and <code class="computeroutput">||</code> (OR), with
76  <code class="computeroutput">bool</code> and <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code>
77  values. For instance:</p>
78<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x = some_op();
79<code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> y = some_other_op();
80if (x &amp;&amp; y) {
81  // both x and y are true
82}
83else if (!(x &amp;&amp; y)) {
84  // either x or y is false
85}
86else {
87  // neither x nor y is false, but we don't know that both are true
88
89  if (x || y) {
90    // either x or y is true
91  }
92}</pre>
93<p> Similarly, <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code>
94  supports 3-state equality comparisons via the operators
95  <code class="computeroutput">==</code> and <code class="computeroutput">!=</code>. These operators differ from
96  "normal" equality operators in C++ because they return a
97  <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code>, because potentially we
98  might not know the result of a comparison (try to compare
99  <code class="computeroutput">true</code> and
100  <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>). For
101  instance:</p>
102<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x(true);
103<code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> y(<code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>);
104
105assert(x == x); // okay, x == x returns true
106assert(x == true); // okay, can compare <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code>s and bools</pre>
107<p> The <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code> keyword (representing the
108  <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code> <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> value)
109  doubles as a function to check if the value of a
110  <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> is indeterminate,
111  e.g.,</p>
112<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x = try_to_do_something_tricky();
113if (<code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>(x)) {
114  // value of x is indeterminate
115}
116else {
117  // report success or failure of x
118}</pre>
119<p> All the logical operators and methods of <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> are marked
120  as <code class="computeroutput">constexpr</code> in C++11. It means that <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> can
121  be used in compile time expressions:</p>
122<pre class="programlisting">constexpr <code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x = (tribool(true) || tribool(indeterminate));
123<code class="computeroutput">static_assert</code>(x, "Must be true!");
124</pre>
125<div class="note"><table border="0" summary="Note">
126<tr>
127<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
128<th align="left">Note</th>
129</tr>
130<tr><td align="left" valign="top">Some compilers may have troubles with evaluating <code class="computeroutput">tribool::operator safe_bool()</code> at compile time.</td></tr>
131</table></div>
132</div>
133<div class="section">
134<div class="titlepage"><div><div><h3 class="title">
135<a name="id-1.3.24.4.3"></a>Renaming the indeterminate state</h3></div></div></div>
136<p> Users may introduce additional keywords for the indeterminate
137  value in addition to the implementation-supplied
138  <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code> using the
139  <code class="computeroutput"><a class="link" href="../BOOST_TRIBOOL_THIRD_STATE.html" title="Macro BOOST_TRIBOOL_THIRD_STATE">BOOST_TRIBOOL_THIRD_STATE</a></code>
140  macro. For instance, the following macro instantiation (at the
141  global scope) will introduce the keyword <code class="computeroutput">maybe</code> as a
142  synonym for <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate.html" title="Function indeterminate">indeterminate</a></code>
143  (also residing in the <code class="computeroutput">boost</code> namespace):</p>
144<pre class="programlisting"><code class="computeroutput"><a class="link" href="../BOOST_TRIBOOL_THIRD_STATE.html" title="Macro BOOST_TRIBOOL_THIRD_STATE">BOOST_TRIBOOL_THIRD_STATE</a></code>(maybe)
145<code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x = maybe;
146if (maybe(x)) { /* ... */ }</pre>
147</div>
148<div class="section">
149<div class="titlepage"><div><div><h3 class="title">
150<a name="id-1.3.24.4.4"></a><code class="computeroutput">tribool</code> input/output</h3></div></div></div>
151<p><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> objects may be
152  read from and written to streams by including the
153  <code class="computeroutput"><a class="link" href="reference.html#header.boost.logic.tribool_io_hpp" title="Header &lt;boost/logic/tribool_io.hpp&gt;">boost/logic/tribool_io.hpp</a></code> header in a
154  manner very similar to <code class="computeroutput">bool</code> values. When the
155  <code class="computeroutput">boolalpha</code> flag is not set on the input/output stream,
156  the integral values 0, 1, and 2 correspond to <code class="computeroutput">tribool</code>
157  values <code class="computeroutput">false</code>, <code class="computeroutput">true</code>, and
158  <code class="computeroutput">indeterminate</code>, respectively. When
159  <code class="computeroutput">boolalpha</code> is set on the stream, arbitrary strings can
160  be used to represent the three values, the default being "false",
161  "true", and "indeterminate". For instance:</p>
162<pre class="programlisting"><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x;
163cin &gt;&gt; x; // Type "0", "1", or "2" to get false, true, or indeterminate
164cout &lt;&lt; boolalpha &lt;&lt; x; // Produces "false", "true", or "indeterminate"</pre>
165<p><code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> input and output
166  is sensitive to the stream's current locale. The strings associated
167  with false and true values are contained in the standard
168  <code class="computeroutput">std::numpunct</code> facet, and the
169  string naming the indeterminate type is contained in the
170  <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate_name.html" title="Class template indeterminate_name">indeterminate_name</a></code> facet. To
171  replace the name of the indeterminate state, you need to imbue your
172  stream with a local containing a
173  <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate_name.html" title="Class template indeterminate_name">indeterminate_name</a></code> facet, e.g.:</p>
174<pre class="programlisting"><code class="computeroutput"><a class="link" href="../BOOST_TRIBOOL_THIRD_STATE.html" title="Macro BOOST_TRIBOOL_THIRD_STATE">BOOST_TRIBOOL_THIRD_STATE</a></code>(maybe)
175locale global;
176locale test_locale(global, new <code class="computeroutput"><a class="link" href="../boost/logic/indeterminate_name.html" title="Class template indeterminate_name">indeterminate_name</a></code>&lt;char&gt;("maybe"));
177cout.imbue(test_locale);
178<code class="computeroutput"><a class="link" href="../boost/logic/tribool.html" title="Class tribool">tribool</a></code> x(maybe);
179cout &lt;&lt; boolalpha &lt;&lt; x &lt;&lt; endl; // Prints "maybe"</pre>
180<p>If you C++ standard library implementation does not support
181  locales, <code class="computeroutput">tribool</code> input/output will still work, but you
182  will be unable to customize the strings printed/parsed when
183  <code class="computeroutput">boolalpha</code> is set.</p>
184</div>
185</div>
186<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
187<td align="left"></td>
188<td align="right"><div class="copyright-footer">Copyright © 2002-2004 Douglas Gregor<p>Use, modification and distribution is subject to the Boost
189    Software License, Version 1.0. (See accompanying file
190    <code class="filename">LICENSE_1_0.txt</code> 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>
191</div></td>
192</tr></table>
193<hr>
194<div class="spirit-nav">
195<a accesskey="p" href="../tribool.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tribool.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="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
196</div>
197</body>
198</html>
199