]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Simplify detection idiom using concepts
authorJonathan Wakely <jwakely@redhat.com>
Fri, 23 Sep 2022 22:16:30 +0000 (23:16 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Sat, 24 Sep 2022 14:18:11 +0000 (15:18 +0100)
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
libstdc++-v3/include/bits/ptr_traits.h
libstdc++-v3/include/experimental/type_traits
libstdc++-v3/include/std/type_traits
libstdc++-v3/testsuite/17_intro/names.cc

index 507e8f1b6b2dabb84fa818563cd842042ad3a9b1..8479bfd612f6ea831a773b441c098f8d6aae5019 100644 (file)
@@ -74,7 +74,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Tp>
       using __pocs = typename _Tp::propagate_on_container_swap;
     template<typename _Tp>
-      using __equal = typename _Tp::is_always_equal;
+      using __equal = __type_identity<typename _Tp::is_always_equal>;
   };
 
   template<typename _Alloc, typename _Up>
@@ -209,7 +209,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        * otherwise @c is_empty<Alloc>::type
       */
       using is_always_equal
-       = __detected_or_t<typename is_empty<_Alloc>::type, __equal, _Alloc>;
+       = typename __detected_or_t<is_empty<_Alloc>, __equal, _Alloc>::type;
 
       template<typename _Tp>
        using rebind_alloc = __alloc_rebind<_Alloc, _Tp>;
index 8360c3b6557d5b7aa5c770e076d6d694d3fba6c6..ae8810706abfe12d9a534bfe1fd1ca30decb7efe 100644 (file)
@@ -144,29 +144,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct __ptr_traits_impl : __ptr_traits_ptr_to<_Ptr, _Elt>
     {
     private:
-      template<typename _Tp, typename = void>
-       struct __difference { using type = ptrdiff_t; };
-
       template<typename _Tp>
-#if __cpp_concepts
-       requires requires { typename _Tp::difference_type; }
-       struct __difference<_Tp>
-#else
-       struct __difference<_Tp, __void_t<typename _Tp::difference_type>>
-#endif
-       { using type = typename _Tp::difference_type; };
-
-      template<typename _Tp, typename _Up, typename = void>
-       struct __rebind : __replace_first_arg<_Tp, _Up> { };
+       using __diff_t = typename _Tp::difference_type;
 
       template<typename _Tp, typename _Up>
-#if __cpp_concepts
-       requires requires { typename _Tp::template rebind<_Up>; }
-       struct __rebind<_Tp, _Up>
-#else
-       struct __rebind<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>>>
-#endif
-       { using type = typename _Tp::template rebind<_Up>; };
+       using __rebind = __type_identity<typename _Tp::template rebind<_Up>>;
 
     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<ptrdiff_t, __diff_t, _Ptr>;
 
       /// A pointer to a different type.
       template<typename _Up>
-        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
index af5970e80d02d8b9caed05b0ed6d55972c0fc20d..fa25a1c2be26e370e6e58ce49370fb69ddcb7474 100644 (file)
@@ -223,7 +223,9 @@ template <typename _Tp, unsigned _Idx = 0>
 
 // See C++14 20.10.6, type relations
 template <typename _Tp, typename _Up>
-  constexpr bool is_same_v = is_same<_Tp, _Up>::value;
+  constexpr bool is_same_v = false;
+template <typename _Tp>
+  constexpr bool is_same_v<_Tp, _Tp> = true;
 template <typename _Base, typename _Derived>
   constexpr bool is_base_of_v = is_base_of<_Base, _Derived>::value;
 template <typename _From, typename _To>
@@ -266,23 +268,21 @@ struct nonesuch : private __nonesuchbase
 };
 #pragma GCC diagnostic pop
 
-template<template<typename...> class _Op, typename... _Args>
-  using is_detected
-    = typename std::__detector<nonesuch, void, _Op, _Args...>::value_t;
-
-template<template<typename...> class _Op, typename... _Args>
-  constexpr bool is_detected_v = is_detected<_Op, _Args...>::value;
-
-template<template<typename...> class _Op, typename... _Args>
-  using detected_t
-    = typename std::__detector<nonesuch, void, _Op, _Args...>::type;
-
 template<typename _Default, template<typename...> class _Op, typename... _Args>
   using detected_or = std::__detected_or<_Default, _Op, _Args...>;
 
 template<typename _Default, template<typename...> class _Op, typename... _Args>
   using detected_or_t = typename detected_or<_Default, _Op, _Args...>::type;
 
+template<template<typename...> class _Op, typename... _Args>
+  using detected_t = detected_or_t<nonesuch, _Op, _Args...>;
+
+template<template<typename...> class _Op, typename... _Args>
+  using is_detected = typename detected_or<void, _Op, _Args...>::__is_detected;
+
+template<template<typename...> class _Op, typename... _Args>
+  constexpr bool is_detected_v = is_detected<_Op, _Args...>::value;
+
 template<typename _Expected, template<typename...> class _Op, typename... _Args>
   using is_detected_exact = is_same<_Expected, detected_t<_Op, _Args...>>;
 
index 7c635313a956a1388363e4d9e297cacb076f88bc..c5853fcad90688def223411acf23d84feb00a62c 100644 (file)
@@ -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<typename _Def, template<typename...> class _Op, typename... _Args>
+    struct __detected_or
+    {
+      using type = _Def;
+      using __is_detected = false_type;
+    };
+
+  // Implementation of the detection idiom (positive case).
+  template<typename _Def, template<typename...> 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<typename _Default, typename _AlwaysVoid,
           template<typename...> 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<typename _Default, template<typename...> 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<typename _Default, template<typename...> class _Op,
index 82e201c71b0c7f52a0a3caa36b3538bd9ef4681e..6490cd63307108f61b43610694cb2f9bd049e89e 100644 (file)
 #define tmp (
 #define sz (
 #define token (
+#define value_t (
 
 #if __cplusplus < 201103L
 #define uses_allocator  (