tribool.boostbook 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <library name="Tribool" dirname="logic" id="tribool"
  5. last-revision="$Date: 2007/05/03 03:28:53 $" xmlns:xi="http://www.w3.org/2001/XInclude">
  6. <libraryinfo>
  7. <author>
  8. <firstname>Douglas</firstname>
  9. <surname>Gregor</surname>
  10. <email>dgregor -at- cs.indiana.edu</email>
  11. </author>
  12. <copyright>
  13. <year>2002</year>
  14. <year>2003</year>
  15. <year>2004</year>
  16. <holder>Douglas Gregor</holder>
  17. </copyright>
  18. <legalnotice>
  19. <para>Use, modification and distribution is subject to the Boost
  20. Software License, Version 1.0. (See accompanying file
  21. <filename>LICENSE_1_0.txt</filename> or copy at <ulink
  22. url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)</para>
  23. </legalnotice>
  24. <librarypurpose>Three-state boolean type</librarypurpose>
  25. <librarycategory name="category:misc"/>
  26. </libraryinfo>
  27. <title>Boost.Tribool</title>
  28. <section id="tribool.introduction">
  29. <title>Introduction</title>
  30. <para>The 3-state boolean library contains a single class,
  31. <code><classname>boost::logic::tribool</classname></code>, along with
  32. support functions and operator overloads that implement 3-state
  33. boolean logic. </para>
  34. </section>
  35. <section id="tribool.tutorial">
  36. <title>Tutorial</title>
  37. <using-namespace name="boost::logic"/>
  38. <section>
  39. <title>Basic usage</title>
  40. <para> The <code><classname>tribool</classname></code> class acts
  41. like the built-in <code>bool</code> type, but for 3-state boolean
  42. logic. The three states are <code>true</code>, <code>false</code>,
  43. and <code><functionname>indeterminate</functionname></code>, where
  44. the first two states are equivalent to those of the C++
  45. <code>bool</code> type and the last state represents an unknown
  46. boolean value (that may be <code>true</code> or
  47. <code>false</code>, we don't know).</para>
  48. <para> The <code><classname>tribool</classname></code> class
  49. supports conversion from <code>bool</code> values and literals
  50. along with its own
  51. <code><functionname>indeterminate</functionname></code>
  52. keyword:</para>
  53. <programlisting><classname>tribool</classname> b(true);
  54. b = false;
  55. b = <functionname>indeterminate</functionname>;
  56. <classname>tribool</classname> b2(b);</programlisting>
  57. <para> <code><classname>tribool</classname></code> supports
  58. conversions to <code>bool</code> for use in conditional
  59. statements. The conversion to <code>bool</code> will be
  60. <code>true</code> when the value of the
  61. <code><classname>tribool</classname></code> is always true, and
  62. <code>false</code> otherwise. Consequently, the following idiom
  63. may be used to determine which of the three states a
  64. <code><classname>tribool</classname></code> currently
  65. holds:</para>
  66. <programlisting><classname>tribool</classname> b = some_operation();
  67. if (b) {
  68. // b is true
  69. }
  70. else if (!b) {
  71. // b is false
  72. }
  73. else {
  74. // b is indeterminate
  75. }</programlisting>
  76. <para> <code><classname>tribool</classname></code> supports the
  77. 3-state logic operators <code>!</code> (negation),
  78. <code>&amp;&amp;</code> (AND), and <code>||</code> (OR), with
  79. <code>bool</code> and <code><classname>tribool</classname></code>
  80. values. For instance:</para>
  81. <programlisting><classname>tribool</classname> x = some_op();
  82. <classname>tribool</classname> y = some_other_op();
  83. if (x &amp;&amp; y) {
  84. // both x and y are true
  85. }
  86. else if (!(x &amp;&amp; y)) {
  87. // either x or y is false
  88. }
  89. else {
  90. // neither x nor y is false, but we don't know that both are true
  91. if (x || y) {
  92. // either x or y is true
  93. }
  94. }</programlisting>
  95. <para> Similarly, <code><classname>tribool</classname></code>
  96. supports 3-state equality comparisons via the operators
  97. <code>==</code> and <code>!=</code>. These operators differ from
  98. "normal" equality operators in C++ because they return a
  99. <code><classname>tribool</classname></code>, because potentially we
  100. might not know the result of a comparison (try to compare
  101. <code>true</code> and
  102. <code><functionname>indeterminate</functionname></code>). For
  103. instance:</para>
  104. <programlisting><classname>tribool</classname> x(true);
  105. <classname>tribool</classname> y(<functionname>indeterminate</functionname>);
  106. assert(x == x); // okay, x == x returns true
  107. assert(x == true); // okay, can compare <classname>tribool</classname>s and bools</programlisting>
  108. <para> The <code><functionname>indeterminate</functionname></code> keyword (representing the
  109. <functionname>indeterminate</functionname>&nbsp;<code><classname>tribool</classname></code> value)
  110. doubles as a function to check if the value of a
  111. <code><classname>tribool</classname></code> is indeterminate,
  112. e.g.,</para>
  113. <programlisting><classname>tribool</classname> x = try_to_do_something_tricky();
  114. if (<functionname>indeterminate</functionname>(x)) {
  115. // value of x is indeterminate
  116. }
  117. else {
  118. // report success or failure of x
  119. }</programlisting>
  120. <para> All the logical operators and methods of <code><classname>tribool</classname></code> are marked
  121. as <code>constexpr</code> in C++11. It means that <code><classname>tribool</classname></code> can
  122. be used in compile time expressions:</para>
  123. <programlisting>constexpr <classname>tribool</classname> x = (tribool(true) || tribool(indeterminate));
  124. <functionname>static_assert</functionname>(x, "Must be true!");
  125. </programlisting>
  126. <note>Some compilers may have troubles with evaluating <code>tribool::operator safe_bool()</code> at compile time.</note>
  127. </section>
  128. <section>
  129. <title>Renaming the indeterminate state</title>
  130. <para> Users may introduce additional keywords for the indeterminate
  131. value in addition to the implementation-supplied
  132. <code><functionname>indeterminate</functionname></code> using the
  133. <code><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname></code>
  134. macro. For instance, the following macro instantiation (at the
  135. global scope) will introduce the keyword <code>maybe</code> as a
  136. synonym for <code><functionname>indeterminate</functionname></code>
  137. (also residing in the <code>boost</code> namespace):</para>
  138. <programlisting><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname>(maybe)
  139. <classname>tribool</classname> x = maybe;
  140. if (maybe(x)) { /* ... */ }</programlisting>
  141. </section>
  142. <section>
  143. <title><code>tribool</code> input/output</title>
  144. <para><code><classname>tribool</classname></code> objects may be
  145. read from and written to streams by including the
  146. <headername>boost/logic/tribool_io.hpp</headername> header in a
  147. manner very similar to <code>bool</code> values. When the
  148. <code>boolalpha</code> flag is not set on the input/output stream,
  149. the integral values 0, 1, and 2 correspond to <code>tribool</code>
  150. values <code>false</code>, <code>true</code>, and
  151. <code>indeterminate</code>, respectively. When
  152. <code>boolalpha</code> is set on the stream, arbitrary strings can
  153. be used to represent the three values, the default being "false",
  154. "true", and "indeterminate". For instance:</para>
  155. <programlisting><classname>tribool</classname> x;
  156. cin &gt;&gt; x; // Type "0", "1", or "2" to get false, true, or indeterminate
  157. cout &lt;&lt; boolalpha &lt;&lt; x; // Produces "false", "true", or "indeterminate"</programlisting>
  158. <para><code><classname>tribool</classname></code> input and output
  159. is sensitive to the stream's current locale. The strings associated
  160. with false and true values are contained in the standard
  161. <code><classname>std::numpunct</classname></code> facet, and the
  162. string naming the indeterminate type is contained in the
  163. <code><classname>indeterminate_name</classname></code> facet. To
  164. replace the name of the indeterminate state, you need to imbue your
  165. stream with a local containing a
  166. <code><classname>indeterminate_name</classname></code> facet, e.g.:</para>
  167. <programlisting><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname>(maybe)
  168. locale global;
  169. locale test_locale(global, new <classname>indeterminate_name</classname>&lt;char&gt;("maybe"));
  170. cout.imbue(test_locale);
  171. <classname>tribool</classname> x(maybe);
  172. cout &lt;&lt; boolalpha &lt;&lt; x &lt;&lt; endl; // Prints "maybe"</programlisting>
  173. <para>If you C++ standard library implementation does not support
  174. locales, <code>tribool</code> input/output will still work, but you
  175. will be unable to customize the strings printed/parsed when
  176. <code>boolalpha</code> is set.</para>
  177. </section>
  178. </section>
  179. <xi:include href="reference.xml"/>
  180. <testsuite id="tribool.tests">
  181. <run-test filename="tribool_test.cpp">
  182. <purpose><para>Test all features of the
  183. <code><classname>boost::logic::tribool</classname></code>
  184. class.</para></purpose>
  185. </run-test>
  186. <run-test filename="tribool_rename_test.cpp">
  187. <purpose><para>Test the use of the
  188. <code><macroname>BOOST_TRIBOOL_THIRD_STATE</macroname></code>
  189. macro.</para></purpose>
  190. </run-test>
  191. <run-test filename="tribool_io_test.cpp">
  192. <purpose><para>Test tribool input/output.</para></purpose>
  193. </run-test>
  194. </testsuite>
  195. </library>