Chapter 27. Boost.Tribool

The library Boost.Tribool provides the class boost::logic::tribool, which is similar to bool. However, while bool can distinguish two states, boost::logic::tribool handles three.

To use boost::logic::tribool, include the header file boost/logic/tribool.hpp.

Example 27.1. Three states of boost::logic::tribool

  1. #include <boost/logic/tribool.hpp>
  2. #include <iostream>
  3. using namespace boost::logic;
  4. int main()
  5. {
  6. tribool b;
  7. std::cout << std::boolalpha << b << '\n';
  8. b = true;
  9. b = false;
  10. b = indeterminate;
  11. if (b)
  12. ;
  13. else if (!b)
  14. ;
  15. else
  16. std::cout << "indeterminate\n";
  17. }

A variable of type boost::logic::tribool can be set to true, false, or indeterminate. The default constructor initializes the variable to false. That’s why Example 27.1 writes false first.

The if statement in Example 27.1 illustrates how to evaluate b correctly. You have to check for true and false explicitly. If the variable is set to indeterminate, as in the example, the else block will be executed.

Boost.Tribool also provides the function boost::logic::indeterminate(). If you pass a variable of type boost::logic::tribool that is set to indeterminate, this function will return true. If the variable is set to true or false, it will return false.

Example 27.2. Logical operators with boost::logic::tribool

  1. #include <boost/logic/tribool.hpp>
  2. #include <boost/logic/tribool_io.hpp>
  3. #include <iostream>
  4. using namespace boost::logic;
  5. int main()
  6. {
  7. std::cout.setf(std::ios::boolalpha);
  8. tribool b1 = true;
  9. std::cout << (b1 || indeterminate) << '\n';
  10. std::cout << (b1 && indeterminate) << '\n';
  11. tribool b2 = false;
  12. std::cout << (b2 || indeterminate) << '\n';
  13. std::cout << (b2 && indeterminate) << '\n';
  14. tribool b3 = indeterminate;
  15. std::cout << (b3 || b3) << '\n';
  16. std::cout << (b3 && b3) << '\n';
  17. }

You can use logical operators with variables of type boost::logic::tribool, just as you can with variables of type bool. In fact, this is the only way to process variables of type boost::logic::tribool because the class doesn’t provide any member functions.

Example 27.2 returns true for b1 || indeterminate, false for b2 && indeterminate, and indeterminate in all other cases. If you look at the operations and their results, you will notice that boost::logic::tribool behaves as one would expect intuitively. The documentation on Boost.Tribool also contains tables that show which operations lead to which results.

Example 27.2 also illustrates how the values true, false, and indeterminate are written to standard output with variables of type boost::logic::tribool. The header file boost/logic/triboolio.hpp must be included and the flag _std::ios::boolalpha must be set for standard output.

Boost.Tribool also provides the macro BOOST_TRIBOOL_THIRD_STATE, which lets you substitute another value for indeterminate. For example, you could use dontknow instead of indeterminate.