C++ Logo

std-discussion

Advanced search

Re: std::optional nullopt comparisons

From: Bjorn Reese <breese_at_[hidden]>
Date: Mon, 24 Jun 2019 17:26:00 +0200
On 6/24/19 3:59 PM, Johan Williamsson via Std-Discussion wrote:

> What's the rationale behind the decision to allow comparisons between
> values and optional values without at least throwing an exception if the
> optional value is not assigned?

Quoting N3672, section "Relational operators":

  "One of the design goals of optional is that objects of type
   optional<T> should be valid elements in STL containers and usable with
   STL algorithms (at least if objects of type T are). Equality
   comparison is essential for optional<T> to model concept Regular. C++
   does not have concepts, but being regular is still essential for the
   type to be effectively used with STL. Ordering is essential if we want
   to store optional values in ordered associative containers. A number
   of ways of including the disengaged state in comparisons have been
   suggested. The ones proposed, have been crafted such that the axioms
   of equivalence and strict weak ordering are preserved: disengaged
   optional<T> is simply treated as an additional and unique value of T
   equal only to itself; this value is always compared as less than any
   value of T"

Received on 2019-06-24 10:27:53