From patchwork Thu Jul 25 10:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1964699 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hfQ1SARi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WV79X3kWyz1yXp for ; Thu, 25 Jul 2024 20:56:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9632A3858C39 for ; Thu, 25 Jul 2024 10:56:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id CB5A03858C66 for ; Thu, 25 Jul 2024 10:55:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB5A03858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CB5A03858C66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721904958; cv=none; b=X0Njo3GFHn5BG94kux5dc7x0xNmqUYndYd8w3znaXHGeI7KC9F4zI36It64vP25WV1lxZ7cFiFnqTcsiATDTUJroQ3oaDD+7wkPNXl0P3sUSf2FcnlpzFX9g5y342cA6Y9JYgI9UrG9FclGve3Eh5EYOFdXqN/VfNFZc8ccGTqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721904958; c=relaxed/simple; bh=JD9pdKKmnvn0rXzk0DIyBnHDoAr/Pqwi6vIUuqr7R6U=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EXKUOT86fpSlwxi4+21zYSvBM42c8r0YQ4xWI4IaRu9wThHFN9Ncm+kL9xA0EZr6e2iTljgRS3HSUipuq9yXlRBBkg/6LOKKDB0FDE8Fnyt6oBQyXTBIEnljDoBmvvtf+YOi1/3G1TtQ5Hp7YnHu8qvnZFxAEqCrx1RbZ79fH0M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721904955; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=zagk8+CEUvBk+tnUmFcNSlqWRRxhKRBKdSn4K62ipik=; b=hfQ1SARiHfEVeXQmq+wJgkfyfpISYjbyOWuN23YsmeDZnopM4uwMQ27dit9HwxaFpy26G6 nq+gbBHLWGQtaBJUbOikLJsnM7sfqewy0Vp8wIWBE8htXqJxMC8BxUh6sFft5Kp1P/g3Ak /SzwTKNMPzSlvyrj0mESFSJuNqmEv2M= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-rYWAm81EOG6_mz6Zf-Wibw-1; Thu, 25 Jul 2024 06:55:52 -0400 X-MC-Unique: rYWAm81EOG6_mz6Zf-Wibw-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 83BA11955F43; Thu, 25 Jul 2024 10:55:50 +0000 (UTC) Received: from localhost (unknown [10.42.28.14]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8B39B195605A; Thu, 25 Jul 2024 10:55:49 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Use concepts and conditional explicit in std::optional Date: Thu, 25 Jul 2024 11:55:30 +0100 Message-ID: <20240725105547.4130530-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- For C++20 mode we can improve compile times by using conditional explicit to reduce the number of constructor overloads. We can also use requires-clauses instead of SFINAE to implement constraints on the constructors and assignment operators. libstdc++-v3/ChangeLog: * include/std/optional (optional): Use C++20 features to simplify overload sets for constructors and assignment operators. --- libstdc++-v3/include/std/optional | 130 +++++++++++++++++++++++++++--- 1 file changed, 121 insertions(+), 9 deletions(-) diff --git a/libstdc++-v3/include/std/optional b/libstdc++-v3/include/std/optional index 700e7047aba..2cc0221865e 100644 --- a/libstdc++-v3/include/std/optional +++ b/libstdc++-v3/include/std/optional @@ -768,6 +768,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION is_assignable<_Tp&, const optional<_Up>&&>, is_assignable<_Tp&, optional<_Up>&&>>; +#if __cpp_concepts && __cpp_conditional_explicit && __glibcxx_remove_cvref +# define _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL 1 +#endif + /** * @brief Class template for optional values. */ @@ -794,17 +798,37 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using _Base = _Optional_base<_Tp>; // SFINAE helpers - template - using __not_self = __not_>>; - template - using __not_tag = __not_>>; - template - using _Requires = enable_if_t<__and_v<_Cond...>, bool>; // _GLIBCXX_RESOLVE_LIB_DEFECTS // 3836. std::expected conversion constructor // expected(const expected&) should take precedence over // expected(U&&) with operator bool +#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + template> + static constexpr bool __not_constructing_bool_from_optional + = true; + + // If T is cv bool, remove_cvref_t is not a specialization of optional + // i.e. do not initialize a bool from optional::operator bool(). + template + static constexpr bool + __not_constructing_bool_from_optional<_From, bool> + = !__is_optional_v>; + + // If T is not cv bool, converts-from-any-cvref> is false. + // The constructor that converts from optional is disabled if the + // contained value can be initialized from optional, so that the + // optional(U&&) constructor can be used instead. + template> + static constexpr bool __construct_from_contained_value + = !__converts_from_optional<_Tp, _From>::value; + + // However, optional can always be converted to bool, so don't apply + // this constraint when T is cv bool. + template + static constexpr bool __construct_from_contained_value<_From, bool> + = true; +#else template> struct __not_constructing_bool_from_optional : true_type @@ -825,6 +849,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : true_type { }; + template + using __not_self = __not_>>; + template + using __not_tag = __not_>>; + template + using __is_bool = is_same, bool>; + template + using _Requires = enable_if_t<__and_v<_Cond...>, bool>; +#endif + public: using value_type = _Tp; @@ -833,6 +867,58 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr optional(nullopt_t) noexcept { } // Converting constructors for engaged optionals. +#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + template + requires (!is_same_v>) + && (!is_same_v>) + && is_constructible_v<_Tp, _Up> + && __not_constructing_bool_from_optional<_Up> + constexpr explicit(!is_convertible_v<_Up, _Tp>) + optional(_Up&& __t) + noexcept(is_nothrow_constructible_v<_Tp, _Up>) + : _Base(std::in_place, std::forward<_Up>(__t)) { } + + template + requires (!is_same_v>) + && is_constructible_v<_Tp, const _Up&> + && __construct_from_contained_value<_Up> + constexpr explicit(!is_convertible_v) + optional(const optional<_Up>& __t) + noexcept(is_nothrow_constructible_v<_Tp, const _Up&>) + { + if (__t) + emplace(__t._M_get()); + } + + template + requires (!is_same_v>) + && is_constructible_v<_Tp, _Up> + && __construct_from_contained_value<_Up> + constexpr explicit(!is_convertible_v<_Up, _Tp>) + optional(optional<_Up>&& __t) + noexcept(is_nothrow_constructible_v<_Tp, _Up>) + { + if (__t) + emplace(std::move(__t._M_get())); + } + + template + requires is_constructible_v<_Tp, _Args...> + explicit constexpr + optional(in_place_t, _Args&&... __args) + noexcept(is_nothrow_constructible_v<_Tp, _Args...>) + : _Base(std::in_place, std::forward<_Args>(__args)...) + { } + + template + requires is_constructible_v<_Tp, initializer_list<_Up>&, _Args...> + explicit constexpr + optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args) + noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, + _Args...>) + : _Base(std::in_place, __il, std::forward<_Args>(__args)...) + { } +#else template, __not_tag<_Up>, is_constructible<_Tp, _Up>, @@ -921,6 +1007,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION noexcept(is_nothrow_constructible_v<_Tp, initializer_list<_Up>&, _Args...>) : _Base(std::in_place, __il, std::forward<_Args>(__args)...) { } +#endif // Assignment operators. _GLIBCXX20_CONSTEXPR optional& @@ -931,13 +1018,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - _GLIBCXX20_CONSTEXPR +#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + requires (!is_same_v>) + && (!(is_scalar_v<_Tp> && is_same_v<_Tp, decay_t<_Up>>)) + && is_constructible_v<_Tp, _Up> + && is_assignable_v<_Tp&, _Up> + constexpr optional& +#else enable_if_t<__and_v<__not_self<_Up>, __not_<__and_, is_same<_Tp, decay_t<_Up>>>>, is_constructible<_Tp, _Up>, is_assignable<_Tp&, _Up>>, optional&> +#endif operator=(_Up&& __u) noexcept(__and_v, is_nothrow_assignable<_Tp&, _Up>>) @@ -951,13 +1045,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - _GLIBCXX20_CONSTEXPR +#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + requires (!is_same_v>) + && is_constructible_v<_Tp, const _Up&> + && is_assignable_v<_Tp&, const _Up&> + && (!__converts_from_optional<_Tp, _Up>::value) + && (!__assigns_from_optional<_Tp, _Up>::value) + constexpr optional& +#else enable_if_t<__and_v<__not_>, is_constructible<_Tp, const _Up&>, is_assignable<_Tp&, const _Up&>, __not_<__converts_from_optional<_Tp, _Up>>, __not_<__assigns_from_optional<_Tp, _Up>>>, optional&> +#endif operator=(const optional<_Up>& __u) noexcept(__and_v, is_nothrow_assignable<_Tp&, const _Up&>>) @@ -977,13 +1079,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } template - _GLIBCXX20_CONSTEXPR +#ifdef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + requires (!is_same_v>) + && is_constructible_v<_Tp, _Up> + && is_assignable_v<_Tp&, _Up> + && (!__converts_from_optional<_Tp, _Up>::value) + && (!__assigns_from_optional<_Tp, _Up>::value) + constexpr optional& +#else enable_if_t<__and_v<__not_>, is_constructible<_Tp, _Up>, is_assignable<_Tp&, _Up>, __not_<__converts_from_optional<_Tp, _Up>>, __not_<__assigns_from_optional<_Tp, _Up>>>, optional&> +#endif operator=(optional<_Up>&& __u) noexcept(__and_v, is_nothrow_assignable<_Tp&, _Up>>) @@ -1654,6 +1764,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template optional(_Tp) -> optional<_Tp>; #endif +#undef _GLIBCXX_USE_CONSTRAINTS_FOR_OPTIONAL + _GLIBCXX_END_NAMESPACE_VERSION } // namespace std