From patchwork Sat Sep 24 14:19:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1433 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp702118wrt; Sat, 24 Sep 2022 07:20:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6w3GKF6O9Z1u3LBBlc50hNFCj6bn9LyXlIKtPbWijM6SLCAImdtdzAvDTTXiV+fRiaZU+T X-Received: by 2002:a17:907:75f4:b0:77c:1b9b:96b3 with SMTP id jz20-20020a17090775f400b0077c1b9b96b3mr11420495ejc.459.1664029213228; Sat, 24 Sep 2022 07:20:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664029213; cv=none; d=google.com; s=arc-20160816; b=tQ4wWIpNytMiKTMEioKY86lm2PoelXMeIPf0C7tgho08XYQUnBP7LD66JjCDBX5rNK wsiQycZPrwlQ1rjv8TvupTvByGF6pM6nyNw6J5LZzRBghV9JEyakLkP99b/EBjFl26DV pst/65xRUBUDeNMC9qymDYuMpHg6FYbok2WOFKdEUpS/vOs+bn8VD0AULbFONjDgcYe3 T6q48wCcRcAH5JFvlCOci9Ftm2W3IlORJO1t58jK7Ugm4rR5oY7ksWdD6MVJiBMQlTQe 0JU8QXEtYW9tHy1jPqLqnI5WXAlUNpNCpZoLFXY8/jDhttpi7XuF3dlqG6vGVDHwkt4C K3DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=YwANe1cwte3MK+udf4RSrz5pJrzT5TiXL7+0OZQAWtk=; b=feU2IFhMUlyE849A/ep3yxJZmZnl+Gv6cAmPf2JPZKGblznD7fguHbtIe8oRJTwaUS Ue3GngSc5VHduqMxh6Q9RXjyyemqOzJm3Ezy0ld6fwAzr318CeCN0X3vfg/Lq3q55Zyg BfFhDTQRXUMgvUkPNR3QRUyUrkzGOtu7pdr1nJwX8s9WEOAHOYgSBz3m4o1jfAHWDz/w vDexXKl89MVcOWup7cq2qpgGN0LEQQk3M5uGuTuJObTMgwyeknuO/n523xb7NM7LUJ0Z MWzOmsXy+kkktHDlhhe4Z+Uwqjrp32EBehS6cOU8WYCurAe9IawtVBHntgJ75gUr4nl7 hUoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=h70k27yv; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hw9-20020a170907a0c900b00781b880f465si6837706ejc.720.2022.09.24.07.20.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Sep 2022 07:20:13 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=h70k27yv; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03CEC3858435 for ; Sat, 24 Sep 2022 14:20:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 03CEC3858435 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664029201; bh=YwANe1cwte3MK+udf4RSrz5pJrzT5TiXL7+0OZQAWtk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=h70k27yvy/JBLAM/5rzBe4Q5AHGcjV20S/qH9u/1ImE/1Rb3+pNxLrbWFVz+DOR57 smFKmbiwMpcDDRqBaNzRlfVlOBqKCSLFQ1GtiHtLGzDvPj5yoKYG4/lO9OHDupfVuz X58MB2gyunFgw+v3+8SYgLTnEKg33uzAJG7rjSZY= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 6FCF63858C53 for ; Sat, 24 Sep 2022 14:19:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6FCF63858C53 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-214-4gEzOka1MN6kGB9mkyqy1g-1; Sat, 24 Sep 2022 10:19:13 -0400 X-MC-Unique: 4gEzOka1MN6kGB9mkyqy1g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 54C69185A78B; Sat, 24 Sep 2022 14:19:13 +0000 (UTC) Received: from localhost (unknown [10.33.36.214]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1A98D140EBF4; Sat, 24 Sep 2022 14:19:13 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Simplify detection idiom using concepts Date: Sat, 24 Sep 2022 15:19:12 +0100 Message-Id: <20220924141912.1892292-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744861096353072028?= X-GMAIL-MSGID: =?utf-8?q?1744861096353072028?= Tested powerpc64le-linux, pushed to trunk. -- >8 -- Add a simpler definition of std::__detected_or using concepts. This also replaces the __detector::value_t member which should have been using a reserved name. Use __detected_or in pointer_traits. libstdc++-v3/ChangeLog: * include/bits/alloc_traits.h (allocator_traits::is_always_equal): Only instantiate is_empty if needed. * include/bits/ptr_traits.h (__ptr_traits_impl::difference_type) (__ptr_traits_impl::rebind): Use __detected_or. * include/experimental/type_traits (is_same_v): Add a partial specialization instead of instantiating the std::is_same class template. (detected_t): Redefine in terms of detected_or_t. (is_detected, is_detected_v): Redefine in terms of detected_t. * include/std/type_traits [__cpp_concepts] (__detected_or): Add new definition using concepts. (__detector::value_t): Rename to __is_detected. * testsuite/17_intro/names.cc: Check value_t isn't used. --- libstdc++-v3/include/bits/alloc_traits.h | 4 +-- libstdc++-v3/include/bits/ptr_traits.h | 27 ++++--------------- libstdc++-v3/include/experimental/type_traits | 24 ++++++++--------- libstdc++-v3/include/std/type_traits | 27 ++++++++++++++++--- libstdc++-v3/testsuite/17_intro/names.cc | 1 + 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/libstdc++-v3/include/bits/alloc_traits.h b/libstdc++-v3/include/bits/alloc_traits.h index 507e8f1b6b2..8479bfd612f 100644 --- a/libstdc++-v3/include/bits/alloc_traits.h +++ b/libstdc++-v3/include/bits/alloc_traits.h @@ -74,7 +74,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template using __pocs = typename _Tp::propagate_on_container_swap; template - using __equal = typename _Tp::is_always_equal; + using __equal = __type_identity; }; template @@ -209,7 +209,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * otherwise @c is_empty::type */ using is_always_equal - = __detected_or_t::type, __equal, _Alloc>; + = typename __detected_or_t, __equal, _Alloc>::type; template using rebind_alloc = __alloc_rebind<_Alloc, _Tp>; diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h index 8360c3b6557..ae8810706ab 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -144,29 +144,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct __ptr_traits_impl : __ptr_traits_ptr_to<_Ptr, _Elt> { private: - template - struct __difference { using type = ptrdiff_t; }; - template -#if __cpp_concepts - requires requires { typename _Tp::difference_type; } - struct __difference<_Tp> -#else - struct __difference<_Tp, __void_t> -#endif - { using type = typename _Tp::difference_type; }; - - template - struct __rebind : __replace_first_arg<_Tp, _Up> { }; + using __diff_t = typename _Tp::difference_type; template -#if __cpp_concepts - requires requires { typename _Tp::template rebind<_Up>; } - struct __rebind<_Tp, _Up> -#else - struct __rebind<_Tp, _Up, __void_t>> -#endif - { using type = typename _Tp::template rebind<_Up>; }; + using __rebind = __type_identity>; public: /// The pointer type. @@ -176,11 +158,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION using element_type = _Elt; /// The type used to represent the difference between two pointers. - using difference_type = typename __difference<_Ptr>::type; + using difference_type = __detected_or_t; /// A pointer to a different type. template - using rebind = typename __rebind<_Ptr, _Up>::type; + using rebind = typename __detected_or_t<__replace_first_arg<_Ptr, _Up>, + __rebind, _Ptr, _Up>::type; }; // _GLIBCXX_RESOLVE_LIB_DEFECTS diff --git a/libstdc++-v3/include/experimental/type_traits b/libstdc++-v3/include/experimental/type_traits index af5970e80d0..fa25a1c2be2 100644 --- a/libstdc++-v3/include/experimental/type_traits +++ b/libstdc++-v3/include/experimental/type_traits @@ -223,7 +223,9 @@ template // See C++14 20.10.6, type relations template - constexpr bool is_same_v = is_same<_Tp, _Up>::value; + constexpr bool is_same_v = false; +template + constexpr bool is_same_v<_Tp, _Tp> = true; template constexpr bool is_base_of_v = is_base_of<_Base, _Derived>::value; template @@ -266,23 +268,21 @@ struct nonesuch : private __nonesuchbase }; #pragma GCC diagnostic pop -template class _Op, typename... _Args> - using is_detected - = typename std::__detector::value_t; - -template class _Op, typename... _Args> - constexpr bool is_detected_v = is_detected<_Op, _Args...>::value; - -template class _Op, typename... _Args> - using detected_t - = typename std::__detector::type; - template class _Op, typename... _Args> using detected_or = std::__detected_or<_Default, _Op, _Args...>; template class _Op, typename... _Args> using detected_or_t = typename detected_or<_Default, _Op, _Args...>::type; +template class _Op, typename... _Args> + using detected_t = detected_or_t; + +template class _Op, typename... _Args> + using is_detected = typename detected_or::__is_detected; + +template class _Op, typename... _Args> + constexpr bool is_detected_v = is_detected<_Op, _Args...>::value; + template class _Op, typename... _Args> using is_detected_exact = is_same<_Expected, detected_t<_Op, _Args...>>; diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits index 7c635313a95..c5853fcad90 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2551,13 +2551,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @cond undocumented + // Detection idiom. + // Detect whether _Op<_Args...> is a valid type, use default _Def if not. + +#if __cpp_concepts + // Implementation of the detection idiom (negative case). + template class _Op, typename... _Args> + struct __detected_or + { + using type = _Def; + using __is_detected = false_type; + }; + + // Implementation of the detection idiom (positive case). + template class _Op, typename... _Args> + requires requires { typename _Op<_Args...>; } + struct __detected_or<_Def, _Op, _Args...> + { + using type = _Op<_Args...>; + using __is_detected = true_type; + }; +#else /// Implementation of the detection idiom (negative case). template class _Op, typename... _Args> struct __detector { - using value_t = false_type; using type = _Default; + using __is_detected = false_type; }; /// Implementation of the detection idiom (positive case). @@ -2565,14 +2586,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION typename... _Args> struct __detector<_Default, __void_t<_Op<_Args...>>, _Op, _Args...> { - using value_t = true_type; using type = _Op<_Args...>; + using __is_detected = true_type; }; - // Detect whether _Op<_Args...> is a valid type, use _Default if not. template class _Op, typename... _Args> using __detected_or = __detector<_Default, void, _Op, _Args...>; +#endif // __cpp_concepts // _Op<_Args...> if that is a valid type, otherwise _Default. template class _Op, diff --git a/libstdc++-v3/testsuite/17_intro/names.cc b/libstdc++-v3/testsuite/17_intro/names.cc index 82e201c71b0..6490cd63307 100644 --- a/libstdc++-v3/testsuite/17_intro/names.cc +++ b/libstdc++-v3/testsuite/17_intro/names.cc @@ -112,6 +112,7 @@ #define tmp ( #define sz ( #define token ( +#define value_t ( #if __cplusplus < 201103L #define uses_allocator (