From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 362E5385828B; Mon, 18 Mar 2024 14:06:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 362E5385828B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710770813; bh=zzBxiE1j5nEF6Pb0dllu0L9fUBiBEIuhfyJH5Wn39SE=; h=From:To:Subject:Date:From; b=gVLzAtfe7pPrM7uedou2VA+CLBhWY/bKqZMQ/W3C8SMlB+OkxXbePNh72MdVFa4rG 47bpVBkMNlNXg6Nazi2Kl8rC5xVYyBhm5oXPL/1Cl96ym8Ie565jOV9TQGdoubgG/h aYh8lE24a8DxUTNbdgE7uUzZJoyqPoDiFMWTvU5k= 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 r12-10261] libstdc++: Simplify detection idiom using concepts X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 51e9dcc664293ee269bf18189646d9dd4e89382d X-Git-Newrev: 1bb467f4910dc4abd90a7be98653b63113e5d6d3 Message-Id: <20240318140653.362E5385828B@sourceware.org> Date: Mon, 18 Mar 2024 14:06:53 +0000 (GMT) List-Id: https://gcc.gnu.org/g:1bb467f4910dc4abd90a7be98653b63113e5d6d3 commit r12-10261-g1bb467f4910dc4abd90a7be98653b63113e5d6d3 Author: Jonathan Wakely Date: Fri Sep 23 23:16:30 2022 +0100 libstdc++: Simplify detection idiom using concepts 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. (cherry picked from commit 2b667beba693e876322af7c6682f9bc885c5ec28) Diff: --- 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 5cc52babef8..25496bc6f83 100644 --- a/libstdc++-v3/include/bits/alloc_traits.h +++ b/libstdc++-v3/include/bits/alloc_traits.h @@ -72,7 +72,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 @@ -207,7 +207,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 d4e7200123c..a6c16709910 100644 --- a/libstdc++-v3/include/std/type_traits +++ b/libstdc++-v3/include/std/type_traits @@ -2632,13 +2632,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). @@ -2646,14 +2667,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 ede2fe8caa7..8c6cd4585cd 100644 --- a/libstdc++-v3/testsuite/17_intro/names.cc +++ b/libstdc++-v3/testsuite/17_intro/names.cc @@ -110,6 +110,7 @@ #define tmp ( #define sz ( #define token ( +#define value_t ( #if __cplusplus < 201103L #define uses_allocator (