On Fri, Feb 28, 2014 at 5:45 AM, Gavin Lambert
On 28/02/2014 12:33, Quoth Frank Mori Hess:
Ah, I wasn't aware optional already had overloaded the comparison operators with comparisons of optional<T> vs T. A little unfortunate IMO, it makes optional<bool> a bit of a disaster when any sort of conversion to bool at all is supported.
Not really; tristate bool logic with optional<bool> is pretty straightforward. It usually falls into the pattern:
if (value == true) { /* do something when true */ } else if (value == false) { /* do something when false */ } else { /* do something when null/none/unknown/unspecified */ }
There is a little trap for the unwary where:
if (value) { /* either true or false */ } else { /* only null, not false */ }
I prefer to check for the value presence first: if (!value) { // value is absent } else if (value.get()) { // value is true } else { // value is false } This way the semantics is pretty obvious.