[libcxx-commits] [libcxx] 1355833 - [libc++] Remove uses of __two in type_traits

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 10 10:57:03 PDT 2022


Author: Nikolas Klauser
Date: 2022-06-10T19:56:59+02:00
New Revision: 13558334f35d1461054a52e1a931f7e096ca2744

URL: https://github.com/llvm/llvm-project/commit/13558334f35d1461054a52e1a931f7e096ca2744
DIFF: https://github.com/llvm/llvm-project/commit/13558334f35d1461054a52e1a931f7e096ca2744.diff

LOG: [libc++] Remove uses of __two in type_traits

Reviewed By: ldionne, #libc

Spies: libcxx-commits

Differential Revision: https://reviews.llvm.org/D127483

Added: 
    

Modified: 
    libcxx/include/__functional/weak_result_type.h
    libcxx/include/__iterator/iterator_traits.h
    libcxx/include/__memory/pointer_traits.h
    libcxx/include/__memory/uses_allocator.h
    libcxx/include/__type_traits/is_class.h
    libcxx/include/__type_traits/is_polymorphic.h
    libcxx/include/__type_traits/is_referenceable.h
    libcxx/include/type_traits

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__functional/weak_result_type.h b/libcxx/include/__functional/weak_result_type.h
index 32484c34b6943..0fd08ee68275d 100644
--- a/libcxx/include/__functional/weak_result_type.h
+++ b/libcxx/include/__functional/weak_result_type.h
@@ -25,11 +25,10 @@ template <class _Tp>
 struct __has_result_type
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Up> static __two __test(...);
-    template <class _Up> static char __test(typename _Up::result_type* = 0);
+    template <class _Up> static false_type __test(...);
+    template <class _Up> static true_type __test(typename _Up::result_type* = 0);
 public:
-    static const bool value = sizeof(__test<_Tp>(0)) == 1;
+    static const bool value = decltype(__test<_Tp>(0))::value;
 };
 
 // __weak_result_type

diff  --git a/libcxx/include/__iterator/iterator_traits.h b/libcxx/include/__iterator/iterator_traits.h
index 991af9abb9b11..c3a5b7e0dd220 100644
--- a/libcxx/include/__iterator/iterator_traits.h
+++ b/libcxx/include/__iterator/iterator_traits.h
@@ -105,15 +105,14 @@ template <class _Tp>
 struct __has_iterator_typedefs
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Up> static __two __test(...);
-    template <class _Up> static char __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
-                                            typename __void_t<typename _Up::
diff erence_type>::type* = 0,
-                                            typename __void_t<typename _Up::value_type>::type* = 0,
-                                            typename __void_t<typename _Up::reference>::type* = 0,
-                                            typename __void_t<typename _Up::pointer>::type* = 0);
+    template <class _Up> static false_type __test(...);
+    template <class _Up> static true_type __test(typename __void_t<typename _Up::iterator_category>::type* = 0,
+                                                 typename __void_t<typename _Up::
diff erence_type>::type* = 0,
+                                                 typename __void_t<typename _Up::value_type>::type* = 0,
+                                                 typename __void_t<typename _Up::reference>::type* = 0,
+                                                 typename __void_t<typename _Up::pointer>::type* = 0);
 public:
-    static const bool value = sizeof(__test<_Tp>(0,0,0,0,0)) == 1;
+    static const bool value = decltype(__test<_Tp>(0,0,0,0,0))::value;
 };
 
 
@@ -121,22 +120,20 @@ template <class _Tp>
 struct __has_iterator_category
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Up> static __two __test(...);
-    template <class _Up> static char __test(typename _Up::iterator_category* = nullptr);
+    template <class _Up> static false_type __test(...);
+    template <class _Up> static true_type __test(typename _Up::iterator_category* = nullptr);
 public:
-    static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
+    static const bool value = decltype(__test<_Tp>(nullptr))::value;
 };
 
 template <class _Tp>
 struct __has_iterator_concept
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Up> static __two __test(...);
-    template <class _Up> static char __test(typename _Up::iterator_concept* = nullptr);
+    template <class _Up> static false_type __test(...);
+    template <class _Up> static true_type __test(typename _Up::iterator_concept* = nullptr);
 public:
-    static const bool value = sizeof(__test<_Tp>(nullptr)) == 1;
+    static const bool value = decltype(__test<_Tp>(nullptr))::value;
 };
 
 #if _LIBCPP_STD_VER > 17

diff  --git a/libcxx/include/__memory/pointer_traits.h b/libcxx/include/__memory/pointer_traits.h
index f1e8070684395..2549e4be7df12 100644
--- a/libcxx/include/__memory/pointer_traits.h
+++ b/libcxx/include/__memory/pointer_traits.h
@@ -71,13 +71,12 @@ template <class _Tp, class _Up>
 struct __has_rebind
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Xp> static __two __test(...);
+    template <class _Xp> static false_type __test(...);
     _LIBCPP_SUPPRESS_DEPRECATED_PUSH
-    template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
+    template <class _Xp> static true_type __test(typename _Xp::template rebind<_Up>* = 0);
     _LIBCPP_SUPPRESS_DEPRECATED_POP
 public:
-    static const bool value = sizeof(__test<_Tp>(0)) == 1;
+    static const bool value = decltype(__test<_Tp>(0))::value;
 };
 
 template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>

diff  --git a/libcxx/include/__memory/uses_allocator.h b/libcxx/include/__memory/uses_allocator.h
index 05c500cd97b13..4a07a4a521219 100644
--- a/libcxx/include/__memory/uses_allocator.h
+++ b/libcxx/include/__memory/uses_allocator.h
@@ -24,11 +24,10 @@ template <class _Tp>
 struct __has_allocator_type
 {
 private:
-    struct __two {char __lx; char __lxx;};
-    template <class _Up> static __two __test(...);
-    template <class _Up> static char __test(typename _Up::allocator_type* = 0);
+    template <class _Up> static false_type __test(...);
+    template <class _Up> static true_type __test(typename _Up::allocator_type* = 0);
 public:
-    static const bool value = sizeof(__test<_Tp>(0)) == 1;
+    static const bool value = decltype(__test<_Tp>(0))::value;
 };
 
 template <class _Tp, class _Alloc, bool = __has_allocator_type<_Tp>::value>

diff  --git a/libcxx/include/__type_traits/is_class.h b/libcxx/include/__type_traits/is_class.h
index 523dc493e8e7f..8e07a5ef070e4 100644
--- a/libcxx/include/__type_traits/is_class.h
+++ b/libcxx/include/__type_traits/is_class.h
@@ -29,12 +29,12 @@ template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_class
 
 namespace __is_class_imp
 {
-template <class _Tp> char  __test(int _Tp::*);
-template <class _Tp> __two __test(...);
+template <class _Tp> true_type  __test(int _Tp::*);
+template <class _Tp> false_type __test(...);
 }
 
 template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_class
-    : public integral_constant<bool, sizeof(__is_class_imp::__test<_Tp>(0)) == 1 && !is_union<_Tp>::value> {};
+    : public integral_constant<bool, decltype(__is_class_imp::__test<_Tp>(0))::value && !is_union<_Tp>::value> {};
 
 #endif
 

diff  --git a/libcxx/include/__type_traits/is_polymorphic.h b/libcxx/include/__type_traits/is_polymorphic.h
index cca7260d04d3f..96da48abeeece 100644
--- a/libcxx/include/__type_traits/is_polymorphic.h
+++ b/libcxx/include/__type_traits/is_polymorphic.h
@@ -18,27 +18,13 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-#if __has_feature(is_polymorphic) || defined(_LIBCPP_COMPILER_MSVC)
-
 template <class _Tp>
 struct _LIBCPP_TEMPLATE_VIS is_polymorphic
     : public integral_constant<bool, __is_polymorphic(_Tp)> {};
 
-#else
-
-template<typename _Tp> char &__is_polymorphic_impl(
-    typename enable_if<sizeof((_Tp*)dynamic_cast<const volatile void*>(declval<_Tp*>())) != 0,
-                       int>::type);
-template<typename _Tp> __two &__is_polymorphic_impl(...);
-
-template <class _Tp> struct _LIBCPP_TEMPLATE_VIS is_polymorphic
-    : public integral_constant<bool, sizeof(__is_polymorphic_impl<_Tp>(0)) == 1> {};
-
-#endif // __has_feature(is_polymorphic)
-
 #if _LIBCPP_STD_VER > 14
 template <class _Tp>
-inline constexpr bool is_polymorphic_v = is_polymorphic<_Tp>::value;
+inline constexpr bool is_polymorphic_v = __is_polymorphic(_Tp);
 #endif
 
 _LIBCPP_END_NAMESPACE_STD

diff  --git a/libcxx/include/__type_traits/is_referenceable.h b/libcxx/include/__type_traits/is_referenceable.h
index 6d98a3db997ea..b97631cc39dd1 100644
--- a/libcxx/include/__type_traits/is_referenceable.h
+++ b/libcxx/include/__type_traits/is_referenceable.h
@@ -19,16 +19,14 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-struct __two {char __lx[2];};
-
 struct __is_referenceable_impl {
     template <class _Tp> static _Tp& __test(int);
-    template <class _Tp> static __two __test(...);
+    template <class _Tp> static false_type __test(...);
 };
 
 template <class _Tp>
 struct __is_referenceable : integral_constant<bool,
-    _IsNotSame<decltype(__is_referenceable_impl::__test<_Tp>(0)), __two>::value> {};
+    _IsNotSame<decltype(__is_referenceable_impl::__test<_Tp>(0)), false_type>::value> {};
 
 _LIBCPP_END_NAMESPACE_STD
 

diff  --git a/libcxx/include/type_traits b/libcxx/include/type_traits
index 8e8d2d47b0f87..44aa4b275795e 100644
--- a/libcxx/include/type_traits
+++ b/libcxx/include/type_traits
@@ -1353,14 +1353,14 @@ struct __is_destructible_apply { typedef int type; };
 template <typename _Tp>
 struct __is_destructor_wellformed {
     template <typename _Tp1>
-    static char  __test (
+    static true_type  __test (
         typename __is_destructible_apply<decltype(declval<_Tp1&>().~_Tp1())>::type
     );
 
     template <typename _Tp1>
-    static __two __test (...);
+    static false_type __test (...);
 
-    static const bool value = sizeof(__test<_Tp>(12)) == sizeof(char);
+    static const bool value = decltype(__test<_Tp>(12))::value;
 };
 
 template <class _Tp, bool>


        


More information about the libcxx-commits mailing list