
Hello,
here's a small boost::logic::tribool riddle:
-<snip>-------------------------------------
#include <iostream>
#include

2011/3/11 Thomas Jarosch
Hello,
here's a small boost::logic::tribool riddle:
-<snip>-------------------------------------
#include <iostream> #include
int main(void) { boost::logic::tribool foo(boost::logic::indeterminate); if (foo != true) { std::cout << "reached1\n"; }
if (foo == boost::logic::indeterminate) { std::cout << "reached2\n"; }
return 0; }
-<snap>-------------------------------------
Try to determine the printed result without looking at the boost::logic::tribool implementation / documentation.
Intuitive?
A coworker just shot himself in the foot...
Best regards, Thomas Jarosch
Without looking in the docs; I havent used tribool in my life ;-) My guess is: 1) foo is intermediate => prints reached2 (cause I expect foo != true to return intermediate) 2) foo is true => prints nothing 3) foo is false => prints reached1 Now I'll check the docs... Regards, Kris

On 11.03.2011 12:53, Thomas Jarosch wrote:
Hello,
here's a small boost::logic::tribool riddle:
-<snip>-------------------------------------
#include<iostream> #include
int main(void) { boost::logic::tribool foo(boost::logic::indeterminate); if (foo != true) { std::cout<< "reached1\n"; }
if (foo == boost::logic::indeterminate) { std::cout<< "reached2\n"; }
return 0; }
-<snap>-------------------------------------
Try to determine the printed result without looking at the boost::logic::tribool implementation / documentation.
Intuitive? If you see tribool as a type that can have the three distinct values "true", "false", and "indeterminate", then no, it's not intuitive. If you instead see tribool as having the two values "true" and "false" and in addition the state "indeterminate", which means "could be true or false, not sure", then the result of the comparison operators is intuitive, but the way they behave in conditions still isn't.
Sebastian

On Friday, 11. March 2011 13:14:13 Sebastian Redl wrote:
if (foo == boost::logic::indeterminate) {
std::cout<< "reached2\n";
...
If you see tribool as a type that can have the three distinct values "true", "false", and "indeterminate", then no, it's not intuitive. If you instead see tribool as having the two values "true" and "false" and in addition the state "indeterminate", which means "could be true or false, not sure", then the result of the comparison operators is intuitive, but the way they behave in conditions still isn't.
Thanks everyone who replied to the riddle. I see this in a way of "three-valued logic" as tribool is homed at boost/logic/tribool.hpp. Boolean logic operations are AND, OR and NOT. Element comparison of a defined set is not a Boolean logic operation. Therefore overriding the comparison operator complicated the use of this library a lot. I showed the example to six programmers and they all got it wrong. Or just have a look at this one: http://stackoverflow.com/questions/3495428/boosttribool-odd-behaviour-or-bug Certainly the original intention was to make life easier for everyone by overriding the operator==(). Unfortunately this doesn't seem to be the case, sophisticated professionals or people unfamiliar with the exact details of the boost::tribool operator==() fail at it. I'm also aware that this interface is in public use and can't be changed easily to avoid usage errors. Or can it? Best regards, Thomas

On Fri, Mar 11, 2011 at 11:53 AM, Thomas Jarosch < thomas.jarosch@intra2net.com> wrote:
Hello,
here's a small boost::logic::tribool riddle:
-<snip>-------------------------------------
#include <iostream> #include
int main(void) { boost::logic::tribool foo(boost::logic::indeterminate); if (foo != true) { std::cout << "reached1\n"; }
if (foo == boost::logic::indeterminate) { std::cout << "reached2\n"; }
return 0; }
-<snap>-------------------------------------
Try to determine the printed result without looking at the boost::logic::tribool implementation / documentation.
Intuitive?
Ok, I guessed, got it wrong, read the docs, and still don't get it!
Modifying
your example to...
#include <iostream>
#include

On 11.03.2011 15:44, Robert Jones wrote:
only prints "reached3", so I can see the logic of second test being indeterminate rather than true, but then surely the comparing indeterminate with indeterminate should also be true?
Now that can only be called a bug. The problem here is that you're comparing two indeterminate keywords, and they're of type indeterminate_keyword_t, which is a function pointer type. No idea why. But this means that it falls back to the built-in comparison operator, which returns a plain true. Sebastian
participants (4)
-
Krzysztof Czainski
-
Robert Jones
-
Sebastian Redl
-
Thomas Jarosch