Boost logo

Boost :

From: Guillaume Melquiond (gmelquio_at_[hidden])
Date: 2002-09-03 04:01:26


On Mon, 2 Sep 2002, Douglas Gregor wrote:

> I mispoke. If the return value were tribool then comparisons would act
> similarly to those with bool return values and the compare_certainly policy
> in boolean contexts, because with something like:
>
> interval<int> i,j; // init i, j
> if (i < j) {
> // ...
> }
>
> would act the same. tribool returns true in a boolean context when the value
> is definitely/certainly true. Unfortunately, this would act differently:
>
> interval<int> i,j; // init i, j
> if (!(i < j)) {
> // ...
> }
>
> compare_certainly would enter the if when it is not certain that i < j,
> whereas tribool would enter the if when it is certain that !(i < j).

Yes, comparisons with intervals are really a strange thing. Here are
some equalities: (the left terms use tribool)

i < j == compare_certainly::(i < j) == cerlt(i, j)
!(i < j) == !compare_possibly::(i < j) == !poslt(i, j)

However, if compare_full is modified in order for it to return a tribool,
then you will have:

i < j == compare_full::(i < j)
!(i < j) == !compare_full::(i < j)

> Essentially, the effect of the policy disappears once the result is converted
> to a bool. So compare_certainly can't state that 'the result of a boolean
> expression involving a comparison is true if the expression is certainly
> true', but is required to state the weaker 'the result of a comparison is
> true if the comparison is certainly true'.

Yes. And it's the reason why the explicit comparison functions where added
to the library: to avoid this kind of misunderstanding.

You could also use compare_full and made it throw an exception in the
intermediary case. And you would be able to state:
- the result of the boolean expression [...] is true if the expression is
  certainly true;
- the result of the boolean expression [...] is false if the expression is
  certainly false;
- an exception is thrown if the expression is uncertain.

Guillaume


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