From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id AE20439AF40F; Fri, 11 Jun 2021 22:25:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE20439AF40F MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-8559] libstdc++: Constrain three-way comparison for std::optional [PR 98842] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 2ba1680d3e9f3bb99783519a08eed6435f131045 X-Git-Newrev: 4f11586945fffd0ff808c16f2f341f9e85d83749 Message-Id: <20210611222557.AE20439AF40F@sourceware.org> Date: Fri, 11 Jun 2021 22:25:57 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Jun 2021 22:25:57 -0000 https://gcc.gnu.org/g:4f11586945fffd0ff808c16f2f341f9e85d83749 commit r11-8559-g4f11586945fffd0ff808c16f2f341f9e85d83749 Author: Jonathan Wakely Date: Mon Jun 7 13:02:15 2021 +0100 libstdc++: Constrain three-way comparison for std::optional [PR 98842] The operator<=>(const optional&, const U&) operator is supposed to be constrained with three_way_comparable_with so that it can only be used when T and U are weakly-equality-comparable and also three-way comparable. Adding that constrain completely breaks std::optional comparisons, because it causes constraint recursion. To avoid that, an additional check that U is not a specialization of std::optional is needed. That appears to be a defect in the standard and should be reported to LWG. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/98842 * include/std/optional (operator<=>(const optional& const U&)): Add missing constraint and add workaround for template recursion. * testsuite/20_util/optional/relops/three_way.cc: Check that type without equality comparison cannot be compared when wrapped in std::optional. (cherry picked from commit adec14811714e22a6c1f7f0199adc05370f0d8b0) Diff: --- libstdc++-v3/include/std/optional | 7 +++++++ .../testsuite/20_util/optional/relops/three_way.cc | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 8b9e038e6e5..415f8c49ef4 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -1234,7 +1234,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return !__rhs || __lhs >= *__rhs; } #ifdef __cpp_lib_three_way_comparison + template + inline constexpr bool __is_optional_v = false; + template + inline constexpr bool __is_optional_v> = true; + template + requires (!__is_optional_v<_Up>) + && three_way_comparable_with<_Tp, _Up> constexpr compare_three_way_result_t<_Tp, _Up> operator<=>(const optional<_Tp>& __x, const _Up& __v) { return bool(__x) ? *__x <=> __v : strong_ordering::less; } diff --git a/libstdc++-v3/testsuite/20_util/optional/relops/three_way.cc b/libstdc++-v3/testsuite/20_util/optional/relops/three_way.cc index 953bef4a1ea..5fc5eec5abf 100644 --- a/libstdc++-v3/testsuite/20_util/optional/relops/three_way.cc +++ b/libstdc++-v3/testsuite/20_util/optional/relops/three_way.cc @@ -15,8 +15,8 @@ // with this library; see the file COPYING3. If not see // . -// { dg-options "-std=gnu++2a" } -// { dg-do compile { target c++2a } } +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } #include @@ -74,3 +74,21 @@ test02() static_assert( nullopt <= O{} ); static_assert( nullopt <= O{1} ); } + +template + concept has_spaceship = requires (const T& t) { t <=> t; }; + +void +test03() +{ + struct E + { + auto operator<=>(const E&) const { return std::strong_ordering::equal; } + }; + static_assert( !std::three_way_comparable ); // not equality comparable + using O = std::optional; + static_assert( !std::three_way_comparable ); + static_assert( ! has_spaceship ); // PR libstdc++/98842 + struct U : O { }; + static_assert( ! has_spaceship ); +}