Boost logo

Boost :

Subject: Re: [boost] [optional] operator<(optional<T>, T) -- is it wrong?
From: David Stone (david_at_[hidden])
Date: 2014-11-26 08:46:48

I have been bitten in the past by operator<. I was converting code that
used unsigned with a 'magic value' to represent nullness to
boost::optional<std::uint32_t>. However, the maximum value it used was
std::numeric_limits<std::uint32_t>:max(). A simple find and replace for
this magic constant (which was referred to everywhere as DWORD_NULL) with
boost::none and fixing compile errors led to some erroneous code. We had
code that assumed an 'uninitialized' std::uint32_t was greater than any
valid value.

The concept of "value not present" is not inherently less-than or
greater-than any value, but because of operator<, code compiled that
shouldn't have. I suppose this is really an argument against any operator<
overload, not just the mixed-type comparison. I don't expect this to be a
major problem for new code, but there is a lot of code out there that uses
a magic value, and that is what optional is supposed to replace.

Boost list run by bdawes at, gregod at, cpdaniel at, john at