diff --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv --- a/libcxx/docs/Status/SpaceshipProjects.csv +++ b/libcxx/docs/Status/SpaceshipProjects.csv @@ -21,8 +21,8 @@ | `[tuple.rel] <https://wg21.link/tuple.rel>`_,| `tuple <https://reviews.llvm.org/D108250>`_,[expos.only.func],Kent Ross,|Complete| "| `[optional.relops] <https://wg21.link/optional.relops>`_ | `[optional.nullops] <https://wg21.link/optional.nullops>`_ -| `[optional.comp.with.t] <https://wg21.link/optional.comp.with.t>`_","| optional -| nullopt",None,Kent Ross,|In Progress| +| `[optional.comp.with.t] <https://wg21.link/optional.comp.with.t>`_","| `optional <https://reviews.llvm.org/D146392>`_ +| `nullopt <https://reviews.llvm.org/D146392>`_",None,Hristo Hristov,|Complete| "| `[variant.relops] <https://wg21.link/variant.relops>`_ | `[variant.monostate.relops] <https://wg21.link/variant.monostate.relops>`_","| `monostate <https://reviews.llvm.org/D131372>`_ | `variant <https://reviews.llvm.org/D131372>`_",None,Kent Ross,|Complete| diff --git a/libcxx/include/optional b/libcxx/include/optional --- a/libcxx/include/optional +++ b/libcxx/include/optional @@ -16,107 +16,125 @@ // C++1z namespace std { - // 23.6.3, optional for object types + // [optional.optional], class template optional template <class T> class optional; - // 23.6.4, no-value state indicator + // [optional.nullopt], no-value state indicator struct nullopt_t{see below }; inline constexpr nullopt_t nullopt(unspecified ); - // 23.6.5, class bad_optional_access + // [optional.bad.access], class bad_optional_access class bad_optional_access; - // 23.6.6, relational operators + // [optional.relops], relational operators template <class T, class U> - constexpr bool operator==(const optional<T>&, const optional<U>&); + constexpr bool operator==(const optional<T>&, const optional<U>&); template <class T, class U> - constexpr bool operator!=(const optional<T>&, const optional<U>&); + constexpr bool operator!=(const optional<T>&, const optional<U>&); template <class T, class U> - constexpr bool operator<(const optional<T>&, const optional<U>&); + constexpr bool operator<(const optional<T>&, const optional<U>&); template <class T, class U> - constexpr bool operator>(const optional<T>&, const optional<U>&); + constexpr bool operator>(const optional<T>&, const optional<U>&); template <class T, class U> - constexpr bool operator<=(const optional<T>&, const optional<U>&); + constexpr bool operator<=(const optional<T>&, const optional<U>&); template <class T, class U> - constexpr bool operator>=(const optional<T>&, const optional<U>&); - - // 23.6.7 comparison with nullopt - template <class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; - template <class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; - template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; - template <class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; - template <class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; - template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; - template <class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; - - // 23.6.8, comparison with T - template <class T, class U> constexpr bool operator==(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator==(const T&, const optional<U>&); - template <class T, class U> constexpr bool operator!=(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator!=(const T&, const optional<U>&); - template <class T, class U> constexpr bool operator<(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator<(const T&, const optional<U>&); - template <class T, class U> constexpr bool operator<=(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator<=(const T&, const optional<U>&); - template <class T, class U> constexpr bool operator>(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator>(const T&, const optional<U>&); - template <class T, class U> constexpr bool operator>=(const optional<T>&, const U&); - template <class T, class U> constexpr bool operator>=(const T&, const optional<U>&); - - // 23.6.9, specialized algorithms - template <class T> void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20 - template <class T> constexpr optional<see below > make_optional(T&&); - template <class T, class... Args> + constexpr bool operator>=(const optional<T>&, const optional<U>&); + template<class T, three_way_comparable_with<T> U> + constexpr compare_three_way_result_t<T, U> + operator<=>(const optional<T>&, const optional<U>&); // since C++20 + + // [optional.nullops], comparison with nullopt + template<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator==(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> constexpr bool operator!=(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator!=(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator<(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> constexpr bool operator<=(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> constexpr bool operator>(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept; // until C++17 + template<class T> constexpr bool operator>=(nullopt_t, const optional<T>&) noexcept; // until C++17 + template<class T> + constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // since C++20 + + // [optional.comp.with.t], comparison with T + template<class T, class U> constexpr bool operator==(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator==(const T&, const optional<U>&); + template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&); + template<class T, class U> constexpr bool operator<(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator<(const T&, const optional<U>&); + template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&); + template<class T, class U> constexpr bool operator>(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator>(const T&, const optional<U>&); + template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&); + template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&); + template<class T, class U> + requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U> + constexpr compare_three_way_result_t<T, U> + operator<=>(const optional<T>&, const U&); // since C++20 + + // [optional.specalg], specialized algorithms + template<class T> + void swap(optional<T>&, optional<T>&) noexcept(see below ); // constexpr in C++20 + + template<class T> + constexpr optional<see below > make_optional(T&&); + template<class T, class... Args> constexpr optional<T> make_optional(Args&&... args); - template <class T, class U, class... Args> + template<class T, class U, class... Args> constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); - // 23.6.10, hash support - template <class T> struct hash; - template <class T> struct hash<optional<T>>; + // [optional.hash], hash support + template<class T> struct hash; + template<class T> struct hash<optional<T>>; - template <class T> class optional { + template<class T> + class optional { public: using value_type = T; - // 23.6.3.1, constructors + // [optional.ctor], constructors constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept; constexpr optional(const optional &); constexpr optional(optional &&) noexcept(see below); - template <class... Args> constexpr explicit optional(in_place_t, Args &&...); - template <class U, class... Args> + template<class... Args> + constexpr explicit optional(in_place_t, Args &&...); + template<class U, class... Args> constexpr explicit optional(in_place_t, initializer_list<U>, Args &&...); - template <class U = T> + template<class U = T> constexpr explicit(see-below) optional(U &&); - template <class U> + template<class U> explicit(see-below) optional(const optional<U> &); // constexpr in C++20 - template <class U> + template<class U> explicit(see-below) optional(optional<U> &&); // constexpr in C++20 - // 23.6.3.2, destructor + // [optional.dtor], destructor ~optional(); // constexpr in C++20 - // 23.6.3.3, assignment - optional &operator=(nullopt_t) noexcept; // constexpr in C++20 + // [optional.assign], assignment + optional &operator=(nullopt_t) noexcept; // constexpr in C++20 constexpr optional &operator=(const optional &); constexpr optional &operator=(optional &&) noexcept(see below); - template <class U = T> optional &operator=(U &&); // constexpr in C++20 - template <class U> optional &operator=(const optional<U> &); // constexpr in C++20 - template <class U> optional &operator=(optional<U> &&); // constexpr in C++20 - template <class... Args> T& emplace(Args &&...); // constexpr in C++20 - template <class U, class... Args> - T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20 - - // 23.6.3.4, swap + template<class U = T> + optional &operator=(U &&); // constexpr in C++20 + template<class U> + optional &operator=(const optional<U> &); // constexpr in C++20 + template<class U> + optional &operator=(optional<U> &&); // constexpr in C++20 + template<class... Args> + T& emplace(Args &&...); // constexpr in C++20 + template<class U, class... Args> + T& emplace(initializer_list<U>, Args &&...); // constexpr in C++20 + + // [optional.swap], swap void swap(optional &) noexcept(see below ); // constexpr in C++20 - // 23.6.3.5, observers + // [optional.observe], observers constexpr T const *operator->() const; constexpr T *operator->(); constexpr T const &operator*() const &; @@ -129,8 +147,8 @@ constexpr T &value() &; constexpr T &&value() &&; constexpr const T &&value() const &&; - template <class U> constexpr T value_or(U &&) const &; - template <class U> constexpr T value_or(U &&) &&; + template<class U> constexpr T value_or(U &&) const &; + template<class U> constexpr T value_or(U &&) &&; // [optional.monadic], monadic operations template<class F> constexpr auto and_then(F&& f) &; // since C++23 @@ -144,15 +162,15 @@ template<class F> constexpr optional or_else(F&& f) &&; // since C++23 template<class F> constexpr optional or_else(F&& f) const&; // since C++23 - // 23.6.3.6, modifiers + // [optional.mod], modifiers void reset() noexcept; // constexpr in C++20 private: - T *val; // exposition only + T *val; // exposition only }; -template<class T> - optional(T) -> optional<T>; + template<class T> + optional(T) -> optional<T>; } // namespace std @@ -160,6 +178,8 @@ #include <__assert> // all public C++ headers provide the assertion handler #include <__availability> +#include <__compare/compare_three_way_result.h> +#include <__compare/three_way_comparable.h> #include <__concepts/invocable.h> #include <__config> #include <__functional/hash.h> @@ -1288,6 +1308,20 @@ return *__x >= *__y; } +#if _LIBCPP_STD_VER >= 20 + +template<class _Tp, three_way_comparable_with<_Tp> _Up> +_LIBCPP_HIDE_FROM_ABI +constexpr compare_three_way_result_t<_Tp, _Up> +operator<=>(const optional<_Tp>& __x, const optional<_Up>& __y) +{ + if (__x && __y) + return *__x <=> *__y; + return __x.has_value() <=> __y.has_value(); +} + +#endif + // Comparisons with nullopt template <class _Tp> _LIBCPP_INLINE_VISIBILITY constexpr @@ -1297,6 +1331,8 @@ return !static_cast<bool>(__x); } +#if _LIBCPP_STD_VER <= 17 + template <class _Tp> _LIBCPP_INLINE_VISIBILITY constexpr bool @@ -1385,6 +1421,18 @@ return !static_cast<bool>(__x); } +#elif _LIBCPP_STD_VER >= 20 + +template<class _Tp> +_LIBCPP_HIDE_FROM_ABI +constexpr strong_ordering +operator<=>(const optional<_Tp>& __x, nullopt_t) noexcept +{ + return __x.has_value() <=> false; +} + +#endif + // Comparisons with T template <class _Tp, class _Up> _LIBCPP_INLINE_VISIBILITY constexpr @@ -1530,6 +1578,19 @@ return static_cast<bool>(__x) ? __v >= *__x : true; } +#if _LIBCPP_STD_VER >= 20 + +template<class _Tp, class _Up> + requires (!__is_std_optional<_Up>::value) && three_way_comparable_with<_Tp, _Up> +_LIBCPP_HIDE_FROM_ABI +constexpr compare_three_way_result_t<_Tp, _Up> +operator<=>(const optional<_Tp>& __x, const _Up& __v) +{ + return __x.has_value() ? *__x <=> __v : strong_ordering::less; +} + +#endif + template <class _Tp> inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 diff --git a/libcxx/test/std/utilities/optional/optional.comp_with_t/compare.three_way.pass.cpp b/libcxx/test/std/utilities/optional/optional.comp_with_t/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.comp_with_t/compare.three_way.pass.cpp @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// <optional> + +// [optional.comp.with.t], comparison with T + +// template<class T, class U> +// requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U> +// constexpr compare_three_way_result_t<T, U> +// operator<=>(const optional<T>&, const U&); + +#include <cassert> +#include <compare> +#include <optional> + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.comp.with.t], comparison with T + { + int t{2}; + std::optional<int> op{3}; + assert((t <=> op) == std::strong_ordering::less); + assert(testOrder(t, op, std::strong_ordering::less)); + } + { + int t{3}; + std::optional<int> op{2}; + assert((t <=> op) == std::strong_ordering::greater); + assert(testOrder(t, op, std::strong_ordering::greater)); + } + { + int t{3}; + std::optional<int> op{3}; + assert((t <=> op) == std::strong_ordering::equal); + assert(testOrder(t, op, std::strong_ordering::equal)); + } + + return true; +} + +int main() { + assert(test()); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/optional/optional.nullops/compare.three_way.pass.cpp b/libcxx/test/std/utilities/optional/optional.nullops/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.nullops/compare.three_way.pass.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// <optional> + +// [optional.nullops], comparison with nullopt + +// template<class T> +// constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; + +#include <cassert> +#include <compare> +#include <optional> + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.nullops], comparison with nullopt + { + std::optional<int> op{1}; + assert((std::nullopt <=> op) == std::strong_ordering::less); + assert(testOrder(std::nullopt, op, std::strong_ordering::less)); + } + { + std::optional<int> op{1}; + assert((op <=> std::nullopt) == std::strong_ordering::greater); + assert(testOrder(op, std::nullopt, std::strong_ordering::greater)); + } + { + std::optional<int> op; + assert((std::nullopt <=> op) == std::strong_ordering::equal); + assert(testOrder(std::nullopt, op, std::strong_ordering::equal)); + assert((op <=> std::nullopt) == std::strong_ordering::equal); + assert(testOrder(op, std::nullopt, std::strong_ordering::equal)); + } + + return true; +} + +int main() { + assert(test()); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/utilities/optional/optional.relops/compare.three_way.pass.cpp b/libcxx/test/std/utilities/optional/optional.relops/compare.three_way.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/optional/optional.relops/compare.three_way.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 + +// <optional> + +// [optional.relops], relational operators + +// template<class T, three_way_comparable_with<T> U> +// constexpr compare_three_way_result_t<T, U> +// operator<=>(const optional<T>&, const optional<U>&); + +#include <cassert> +#include <compare> +#include <optional> + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.relops], relational operators + std::optional<int> op; + + { + std::optional<int> op1{2}; + std::optional<int> op2{3}; + assert((op <=> op2) == std::strong_ordering::less); + assert(testOrder(op, op2, std::strong_ordering::less)); + assert((op1 <=> op2) == std::strong_ordering::less); + assert(testOrder(op1, op2, std::strong_ordering::less)); + } + { + std::optional<int> op1{3}; + std::optional<int> op2{2}; + assert((op1 <=> op) == std::strong_ordering::greater); + assert(testOrder(op1, op, std::strong_ordering::greater)); + assert((op1 <=> op2) == std::strong_ordering::greater); + assert(testOrder(op1, op2, std::strong_ordering::greater)); + } + { + assert((op <=> op) == std::strong_ordering::equal); + assert(testOrder(op, op, std::strong_ordering::equal)); + + std::optional<int> op1{3}; + std::optional<int> op2{3}; + assert((op1 <=> op1) == std::strong_ordering::equal); + assert(testOrder(op1, op1, std::strong_ordering::equal)); + assert((op1 <=> op2) == std::strong_ordering::equal); + assert(testOrder(op1, op2, std::strong_ordering::equal)); + } + + return true; +} + +int main() { + assert(test()); + static_assert(test()); + return 0; +}