This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.
optional<const T>
doesn't compare with T
Section: 22.5.8 [optional.comp.with.t] Status: C++17 Submitter: Ville Voutilainen Opened: 2017-02-17 Last modified: 2021-06-06
Priority: Not Prioritized
View all other issues in [optional.comp.with.t].
View all issues with C++17 status.
Discussion:
Consider:
optional<const int> x = 42; int y = 666; x == y; // ill-formed
The comparison is ill-formed, because in [optional.comp_with_t]/1,
template <class T> constexpr bool operator==(const optional<T>& x, const T& v);
the T
is deduced to be both const int
and int
, which is ill-formed.
Since it became apparent that the root cause of this issue is also causing difficulties with e.g.
comparing optional<string>
with literals etc., here's a p/r that
adds requirements for optional
's comparisons with T
turns optional
-optional
comparisons into two-template-parameter
templates, allowing comparing mixed optionals
turns optional
-T
comparisons into two-template-parameter templates,
allowing comparing optionals with T
and types comparable with T
[Kona 2017-02-28]
Accepted as Immediate to avoid having break ABI later.
Proposed resolution:
This wording is relative to N4640.
Modify 22.5.2 [optional.syn], header <optional>
synopsis, as indicated:
[…] // 22.5.6 [optional.relops], relational operators template <class T> constexpr bool operator==(const optional<T>&, const optional<T>&); template <class T> constexpr bool operator!=(const optional<T>&, const optional<T>&); template <class T> constexpr bool operator<(const optional<T>&, const optional<T>&); template <class T> constexpr bool operator>(const optional<T>&, const optional<T>&); template <class T> constexpr bool operator<=(const optional<T>&, const optional<T>&); template <class T> constexpr bool operator>=(const optional<T>&, const optional<T>&); […] // [optional.comp_with_t], comparison with T template <class T> constexpr bool operator==(const optional<T>&, constT&); template <class T> constexpr bool operator==(constT&, const optional<T>&); template <class T> constexpr bool operator!=(const optional<T>&, constT&); template <class T> constexpr bool operator!=(constT&, const optional<T>&); template <class T> constexpr bool operator<(const optional<T>&, constT&); template <class T> constexpr bool operator<(constT&, const optional<T>&); template <class T> constexpr bool operator<=(const optional<T>&, constT&); template <class T> constexpr bool operator<=(constT&, const optional<T>&); template <class T> constexpr bool operator>(const optional<T>&, constT&); template <class T> constexpr bool operator>(constT&, const optional<T>&); template <class T> constexpr bool operator>=(const optional<T>&, constT&); template <class T> constexpr bool operator>=(constT&, const optional<T>&);
Modify 22.5.6 [optional.relops] as indicated:
template <class T> constexpr bool operator==(const optional<T>& x, const optional<T>& y);[…]
template <class T> constexpr bool operator!=(const optional<T>& x, const optional<T>& y);[…]
template <class T> constexpr bool operator<(const optional<T>& x, const optional<T>& y);[…]
template <class T> constexpr bool operator>(const optional<T>& x, const optional<T>& y);[…]
template <class T> constexpr bool operator<=(const optional<T>& x, const optional<T>& y);[…]
template <class T> constexpr bool operator>=(const optional<T>& x, const optional<T>& y);[…]
Modify [optional.comp_with_t] as indicated:
template <class T> constexpr bool operator==(const optional<T>& x, constT& v);-1- Effects: Equivalent to:
return bool(x) ? *x == v : false;
template <class T> constexpr bool operator==(constT& v, const optional<T>& x);-2- Effects: Equivalent to:
return bool(x) ? v == *x : false;
template <class T> constexpr bool operator!=(const optional<T>& x, constT& v);-3- Effects: Equivalent to:
return bool(x) ? *x != v : true;
template <class T> constexpr bool operator!=(constT& v, const optional<T>& x);-4- Effects: Equivalent to:
return bool(x) ? v != *x : true;
template <class T> constexpr bool operator<(const optional<T>& x, constT& v);-5- Effects: Equivalent to:
return bool(x) ? *x < v : true;
template <class T> constexpr bool operator<(constT& v, const optional<T>& x);-6- Effects: Equivalent to:
return bool(x) ? v < *x : false;
template <class T> constexpr bool operator<=(const optional<T>& x, constT& v);-7- Effects: Equivalent to:
return bool(x) ? *x <= v : true;
template <class T> constexpr bool operator<=(constT& v, const optional<T>& x);-8- Effects: Equivalent to:
return bool(x) ? v <= *x : false;
template <class T> constexpr bool operator>(const optional<T>& x, constT& v);-9- Effects: Equivalent to:
return bool(x) ? *x > v : false;
template <class T> constexpr bool operator>(constT& v, const optional<T>& x);-10- Effects: Equivalent to:
return bool(x) ? v > *x : true;
template <class T> constexpr bool operator>=(const optional<T>& x, constT& v);-11- Effects: Equivalent to:
return bool(x) ? *x >= v : false;
template <class T> constexpr bool operator>=(constT& v, const optional<T>& x);-12- Effects: Equivalent to:
return bool(x) ? v >= *x : true;