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