From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 72D1F3840C16 for ; Thu, 5 Nov 2020 19:50:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 72D1F3840C16 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-428-WyHXGLETNp2XCTVhG-ZiVA-1; Thu, 05 Nov 2020 14:50:51 -0500 X-MC-Unique: WyHXGLETNp2XCTVhG-ZiVA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C0A1A802B4C; Thu, 5 Nov 2020 19:50:50 +0000 (UTC) Received: from localhost (unknown [10.33.36.7]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3CD9B21E94; Thu, 5 Nov 2020 19:50:49 +0000 (UTC) Date: Thu, 5 Nov 2020 19:50:49 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: Re: [committed] libstdc++: Fix constraints on std::optional comparisons [PR 96269] Message-ID: <20201105195049.GI503596@redhat.com> References: <20201105190941.GA3537038@redhat.com> MIME-Version: 1.0 In-Reply-To: <20201105190941.GA3537038@redhat.com> X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="UFMLoheMaWcIEZAi" Content-Disposition: inline X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 05 Nov 2020 19:50:55 -0000 --UFMLoheMaWcIEZAi Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On 05/11/20 19:09 +0000, Jonathan Wakely wrote: >The relational operators for std::optional were using the wrong types >in the declval expressions used to constrain them. Instead of using >const lvalues they were using non-const rvalues, which meant that a type >might satisfy the constraints but then give an error when the function >body was instantiated. > >libstdc++-v3/ChangeLog: > > PR libstdc++/96269 > * include/std/optional (operator==, operator!=, operator<) > (operator>, operator<=, operator>=): Fix types used in > SFINAE constraints. > * testsuite/20_util/optional/relops/96269.cc: New test. > >Tested powerpc64le-linux. Committed to trunk. When concepts are supported we can make the alias templates __optional_eq_t et al use a requires-expression instead of SFINAE. This is potentially faster to compile, given expected improvements to C++20 compilers. I'm testing this patch. --UFMLoheMaWcIEZAi Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit c5d8e2ba0ad20425cc7778152824d9e5267b0ec5 Author: Jonathan Wakely Date: Thu Nov 5 19:45:52 2020 libstdc++: Use concepts to constrain std::optional relops When concepts are supported we can make the alias templates __optional_eq_t et al use a requires-expression instead of SFINAE. This is potentially faster to compile, given expected improvements to C++20 compilers. libstdc++-v3/ChangeLog: * include/std/optional [__cpp_concepts] (__optional_eq_t) (__optional_ne_t, __optional_lt_t, __optional_gt_t) (__optional_le_t, __optional_ge_t): Use requires-clause on alias template. diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 5ea5b39d0e69..4e9618648250 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -998,9 +998,48 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void reset() noexcept { this->_M_reset(); } }; +#if __cpp_lib_concepts + template + requires requires (const _Tp __t, const _Up __u) { + { __t == __u } -> convertible_to; + } + using __optional_eq_t = bool; + + template + requires requires (const _Tp __t, const _Up __u) { + { __t != __u } -> convertible_to; + } + using __optional_ne_t = bool; + + template + requires requires (const _Tp __t, const _Up __u) { + { __t < __u } -> convertible_to; + } + using __optional_lt_t = bool; + + template + requires requires (const _Tp __t, const _Up __u) { + { __t > __u } -> convertible_to; + } + using __optional_gt_t = bool; + + template + requires requires (const _Tp __t, const _Up __u) { + { __t <= __u } -> convertible_to; + } + using __optional_le_t = bool; + + template + requires requires (const _Tp __t, const _Up __u) { + { __t >= __u } -> convertible_to; + } + using __optional_ge_t = bool; + +#else // concepts + template using __optional_relop_t = - enable_if_t::value, bool>; + enable_if_t, bool>; template using __optional_eq_t = __optional_relop_t< @@ -1031,6 +1070,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using __optional_ge_t = __optional_relop_t< decltype(std::declval() >= std::declval()) >; +#endif // concepts // Comparisons between optional values. template --UFMLoheMaWcIEZAi--