Boost logo

Boost Users :

Subject: [Boost-users] boost::tribool "if ( someTriboolVar == indeterminate )" semantics is confusing
From: Peter Hackett (peter.hackett_at_[hidden])
Date: 2014-03-05 23:06:47


I started using tribool recently. I just discovered that I was using it
incorrectly.

I had code that did something like:

     tribool triboolVar1 = indeterminate;

     if ( triboolVar1 == indeterminate ) {
         cout << "triboolVar1 is indeterminate" << endl;
     } else {
         cout << "triboolVar1 is NOT indeterminate" << endl;
     }

I was expecting this to print "triboolVar1 is indeterminate".
However it prints "triboolVar1 is NOT indeterminate".

I ended up looking through "include/boost/logic/tribool.hpp" and after
a bit of head scratching, I think I understand why this doesn't do
what I expect:

The above "if" line is "really" (pseudo-code):

if ( operator==(tribool triboolVar1,tribool indeterminate) ) {

where operator== returns a tribool, and (I think)

tribool::operator safe_bool() const
   {
     return value == true_value? &dummy::nonnull : 0;
   }

is used to turn the tribool into a bool.

And give that operator== says:

...
if (indeterminate(x) || indeterminate(y))
     return indeterminate;
...

Then clearly no matter what the value of triboolVar1 is, the "if" code
will be false.

I haven't had any schooling in what (I'm assuming) might be called something
like "Multi-Level-Logic", but:

Shouldn't an "indeterminate" value equal another "indeterminate" value?

-- 
Peter Hackett
Member of Technical Staff
IC Manage http://icmanage.com
phone (408) 358-8191 x6012

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net