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] `_,| `tuple `_,[expos.only.func],Kent Ross,|Complete| "| `[optional.relops] `_ | `[optional.nullops] `_ -| `[optional.comp.with.t] `_","| optional -| nullopt",None,Kent Ross,|In Progress| +| `[optional.comp.with.t] `_","| `optional `_ +| `nullopt `_",None,Hristo Hristov,|Complete| "| `[variant.relops] `_ | `[variant.monostate.relops] `_","| `monostate `_ | `variant `_",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 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 - constexpr bool operator==(const optional&, const optional&); + constexpr bool operator==(const optional&, const optional&); template - constexpr bool operator!=(const optional&, const optional&); + constexpr bool operator!=(const optional&, const optional&); template - constexpr bool operator<(const optional&, const optional&); + constexpr bool operator<(const optional&, const optional&); template - constexpr bool operator>(const optional&, const optional&); + constexpr bool operator>(const optional&, const optional&); template - constexpr bool operator<=(const optional&, const optional&); + constexpr bool operator<=(const optional&, const optional&); template - constexpr bool operator>=(const optional&, const optional&); - - // 23.6.7 comparison with nullopt - template constexpr bool operator==(const optional&, nullopt_t) noexcept; - template constexpr bool operator==(nullopt_t, const optional&) noexcept; - template constexpr bool operator!=(const optional&, nullopt_t) noexcept; - template constexpr bool operator!=(nullopt_t, const optional&) noexcept; - template constexpr bool operator<(const optional&, nullopt_t) noexcept; - template constexpr bool operator<(nullopt_t, const optional&) noexcept; - template constexpr bool operator<=(const optional&, nullopt_t) noexcept; - template constexpr bool operator<=(nullopt_t, const optional&) noexcept; - template constexpr bool operator>(const optional&, nullopt_t) noexcept; - template constexpr bool operator>(nullopt_t, const optional&) noexcept; - template constexpr bool operator>=(const optional&, nullopt_t) noexcept; - template constexpr bool operator>=(nullopt_t, const optional&) noexcept; - - // 23.6.8, comparison with T - template constexpr bool operator==(const optional&, const U&); - template constexpr bool operator==(const T&, const optional&); - template constexpr bool operator!=(const optional&, const U&); - template constexpr bool operator!=(const T&, const optional&); - template constexpr bool operator<(const optional&, const U&); - template constexpr bool operator<(const T&, const optional&); - template constexpr bool operator<=(const optional&, const U&); - template constexpr bool operator<=(const T&, const optional&); - template constexpr bool operator>(const optional&, const U&); - template constexpr bool operator>(const T&, const optional&); - template constexpr bool operator>=(const optional&, const U&); - template constexpr bool operator>=(const T&, const optional&); - - // 23.6.9, specialized algorithms - template void swap(optional&, optional&) noexcept(see below ); // constexpr in C++20 - template constexpr optional make_optional(T&&); - template + constexpr bool operator>=(const optional&, const optional&); + template U> + constexpr compare_three_way_result_t + operator<=>(const optional&, const optional&); // since C++20 + + // [optional.nullops], comparison with nullopt + template constexpr bool operator==(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator==(nullopt_t, const optional&) noexcept; // until C++17 + template constexpr bool operator!=(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator!=(nullopt_t, const optional&) noexcept; // until C++17 + template constexpr bool operator<(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator<(nullopt_t, const optional&) noexcept; // until C++17 + template constexpr bool operator<=(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator<=(nullopt_t, const optional&) noexcept; // until C++17 + template constexpr bool operator>(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator>(nullopt_t, const optional&) noexcept; // until C++17 + template constexpr bool operator>=(const optional&, nullopt_t) noexcept; // until C++17 + template constexpr bool operator>=(nullopt_t, const optional&) noexcept; // until C++17 + template + constexpr strong_ordering operator<=>(const optional&, nullopt_t) noexcept; // since C++20 + + // [optional.comp.with.t], comparison with T + template constexpr bool operator==(const optional&, const U&); + template constexpr bool operator==(const T&, const optional&); + template constexpr bool operator!=(const optional&, const U&); + template constexpr bool operator!=(const T&, const optional&); + template constexpr bool operator<(const optional&, const U&); + template constexpr bool operator<(const T&, const optional&); + template constexpr bool operator<=(const optional&, const U&); + template constexpr bool operator<=(const T&, const optional&); + template constexpr bool operator>(const optional&, const U&); + template constexpr bool operator>(const T&, const optional&); + template constexpr bool operator>=(const optional&, const U&); + template constexpr bool operator>=(const T&, const optional&); + template + requires (!is-derived-from-optional) && three_way_comparable_with + constexpr compare_three_way_result_t + operator<=>(const optional&, const U&); // since C++20 + + // [optional.specalg], specialized algorithms + template + void swap(optional&, optional&) noexcept(see below ); // constexpr in C++20 + + template + constexpr optional make_optional(T&&); + template constexpr optional make_optional(Args&&... args); - template + template constexpr optional make_optional(initializer_list il, Args&&... args); - // 23.6.10, hash support - template struct hash; - template struct hash>; + // [optional.hash], hash support + template struct hash; + template struct hash>; - template class optional { + template + 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 constexpr explicit optional(in_place_t, Args &&...); - template + template + constexpr explicit optional(in_place_t, Args &&...); + template constexpr explicit optional(in_place_t, initializer_list, Args &&...); - template + template constexpr explicit(see-below) optional(U &&); - template + template explicit(see-below) optional(const optional &); // constexpr in C++20 - template + template explicit(see-below) optional(optional &&); // 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 optional &operator=(U &&); // constexpr in C++20 - template optional &operator=(const optional &); // constexpr in C++20 - template optional &operator=(optional &&); // constexpr in C++20 - template T& emplace(Args &&...); // constexpr in C++20 - template - T& emplace(initializer_list, Args &&...); // constexpr in C++20 - - // 23.6.3.4, swap + template + optional &operator=(U &&); // constexpr in C++20 + template + optional &operator=(const optional &); // constexpr in C++20 + template + optional &operator=(optional &&); // constexpr in C++20 + template + T& emplace(Args &&...); // constexpr in C++20 + template + T& emplace(initializer_list, 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 constexpr T value_or(U &&) const &; - template constexpr T value_or(U &&) &&; + template constexpr T value_or(U &&) const &; + template constexpr T value_or(U &&) &&; // [optional.monadic], monadic operations template constexpr auto and_then(F&& f) &; // since C++23 @@ -144,15 +162,15 @@ template constexpr optional or_else(F&& f) &&; // since C++23 template 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 - optional(T) -> optional; + template + optional(T) -> optional; } // 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 _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 _LIBCPP_INLINE_VISIBILITY constexpr @@ -1297,6 +1331,8 @@ return !static_cast(__x); } +#if _LIBCPP_STD_VER <= 17 + template _LIBCPP_INLINE_VISIBILITY constexpr bool @@ -1385,6 +1421,18 @@ return !static_cast(__x); } +#elif _LIBCPP_STD_VER >= 20 + +template +_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 _LIBCPP_INLINE_VISIBILITY constexpr @@ -1530,6 +1578,19 @@ return static_cast(__x) ? __v >= *__x : true; } +#if _LIBCPP_STD_VER >= 20 + +template + 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 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.comp.with.t], comparison with T + +// template +// requires (!is-derived-from-optional) && three_way_comparable_with +// constexpr compare_three_way_result_t +// operator<=>(const optional&, const U&); + +#include +#include +#include + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.comp.with.t], comparison with T + { + int t{2}; + std::optional op{3}; + assert((t <=> op) == std::strong_ordering::less); + assert(testOrder(t, op, std::strong_ordering::less)); + } + { + int t{3}; + std::optional op{2}; + assert((t <=> op) == std::strong_ordering::greater); + assert(testOrder(t, op, std::strong_ordering::greater)); + } + { + int t{3}; + std::optional 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.nullops], comparison with nullopt + +// template +// constexpr strong_ordering operator<=>(const optional&, nullopt_t) noexcept; + +#include +#include +#include + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.nullops], comparison with nullopt + { + std::optional op{1}; + assert((std::nullopt <=> op) == std::strong_ordering::less); + assert(testOrder(std::nullopt, op, std::strong_ordering::less)); + } + { + std::optional op{1}; + assert((op <=> std::nullopt) == std::strong_ordering::greater); + assert(testOrder(op, std::nullopt, std::strong_ordering::greater)); + } + { + std::optional 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.relops], relational operators + +// template U> +// constexpr compare_three_way_result_t +// operator<=>(const optional&, const optional&); + +#include +#include +#include + +#include "test_comparisons.h" + +constexpr bool test() { + // [optional.relops], relational operators + std::optional op; + + { + std::optional op1{2}; + std::optional 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 op1{3}; + std::optional 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 op1{3}; + std::optional 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; +}