Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-08-01 11:37:08


I've just placed a 3-state boolean type in the sandbox, available here:
  
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/boost-sandbox/boost-sandbox/boost/tribool.hpp?rev=HEAD&content-type=text/vnd.viewcvs-markup

The type, tribool, can be either true, false, or indeterminate. It follows the
normal rules for 3-valued logic, and supports all boolean operations (==, !=,
!, &&, ||), including conversions from bool and conversion to bool (via the
safe_bool idiom).

Usage scenario:

  tribool result = some_trivalued_boolean_function();
  if (result) {
    // value is 'true'
  }
  else if (!result) {
    // value is 'false'
  }
  else {
    // value is indeterminate
  }

  bool b = check_something();
  if (result || b) {
    // if result is definitely true or if b is true
  }

  if (result == indeterminate) {
    // well, now what? result is indeterminate
  }

There's a testcase in boost-sandbox/libs/utility/test to exercise most (all?)
of the tribool/bool combinations.

If anyone is interested in the tribool type, I'd be happy to document it and
put it up for review. (Or, just add it to the utility library, it's quite
small).

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk