boost tribool

boost::logic::tribool is similar to bool. However, while bool can distinguish two states, boost::logic::tribool handles three.

#include <boost/logic/tribool.hpp>
#include <boost/logic/tribool_io.hpp>
#include <iostream>

using namespace boost::logic;

int main() {
  std::cout.setf(std::ios::boolalpha);

  tribool b1 = true;
  std::cout << (b1 || indeterminate) << std::endl;
  std::cout << (b1 && indeterminate) << std::endl;

  tribool b2 = false;
  std::cout << (b2 || indeterminate) << std::endl;
  std::cout << (b2 && indeterminate) << std::endl;

  tribool b3 = indeterminate;
  std::cout << (b3 || b3) << std::endl;
  std::cout << (b3 && b3) << std::endl;

  return 0;
}

输出为:

true

indeterminate

indeterminate

false

indeterminate

indeterminate

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 only way to process variables of type boost::logic::tribool because the class dosen't provide any member functions.

猜你喜欢

转载自www.cnblogs.com/sssblog/p/11122721.html
今日推荐