[libcxx-commits] [libcxx] Make std::nullopt_t comparable (PR #195549)
A. Jiang via libcxx-commits
libcxx-commits at lists.llvm.org
Wed May 6 23:00:18 PDT 2026
================
@@ -362,6 +362,16 @@ struct nullopt_t {
explicit __secret_tag() = default;
};
_LIBCPP_HIDE_FROM_ABI constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
+# if _LIBCPP_STD_VER >= 20
+ _LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering operator<=>(const nullopt_t&, const nullopt_t&) = default;
+# else
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator==(const nullopt_t&, const nullopt_t) { return true; }
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator!=(const nullopt_t&, const nullopt_t) { return false; }
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator>(const nullopt_t&, const nullopt_t) { return false; }
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator<(const nullopt_t&, const nullopt_t) { return false; }
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator>=(const nullopt_t&, const nullopt_t) { return true; }
+ _LIBCPP_HIDE_FROM_ABI constexpr friend bool operator<=(const nullopt_t&, const nullopt_t) { return true; }
+# endif
----------------
frederick-vs-ja wrote:
Oops, it's weird to use parameter list `(const nullopt_t&, const nullopt_t)`. Perhaps it would be better to just pass `nullopt_t` by value, which is consistent with operators for `monostate`. Also, perhaps would also be better to add `noexcept`.
See also [[variant.monostate.relops]](https://eel.is/c++draft/variant.monostate.relops), [N4659 [variant.monostate.relops]](https://timsong-cpp.github.io/cppwp/n4659/variant.monostate.relops).
https://github.com/llvm/llvm-project/pull/195549
More information about the libcxx-commits
mailing list