6

I'm exploring boost::tribool and was surprised by the following behaviour.

{
using namespace boost;

boost::tribool t(indeterminate);

assert(t==indeterminate);  // This assertion fails!
} 

However, if I do this, the assert passes.

assert(indeterminate(t));

No compiler warnings or errors in either case. Anyone have a good explanation of why this should be??

Roddy
  • 66,617
  • 42
  • 165
  • 277

1 Answers1

11

I think the answer is in the documentation:

the result of comparing two indeterminate values is indeterminate (not true) - we don't know what the values are, so we can't tell that they are equal;

the indeterminate function can be used to test if a tribool is in an indeterminate state.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • And since indeterminate converts implicitly to bool false, the assert fails. – Mark B Aug 16 '10 at 17:13
  • 1
    "the result of comparing two indeterminate values is indeterminate". Ouch. I guess that makes sense at one level, but it's not intuitive to me. – Roddy Aug 16 '10 at 19:02
  • @Roddy, look at implementation of tribool::operator==() are u ready? it returns tribool! I hate the guy who submitted this code to boost :D +1 for good question, this is NOT intuitive behavior! – Hovhannes Grigoryan Apr 19 '11 at 19:46
  • @HovhannesGrigoryan once you learn to deal with indeterminate values (`R` is an example of a language which has them for all types), you realize that this is the only option that makes sense – eddi Jun 29 '15 at 16:17