[libcxx-commits] [libcxx] cb48ed3 - [libc++][NFCI] span: replace enable_if with concepts

Joe Loser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 14 20:26:58 PDT 2022


Author: Joe Loser
Date: 2022-06-14T21:25:50-06:00
New Revision: cb48ed38b8d09ef9cfa1f7258342d0379f169074

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

LOG: [libc++][NFCI] span: replace enable_if with concepts

Several span constructors use `enable_if` which is verbose. Replace these with
concepts or requires expressions.

Added: 
    

Modified: 
    libcxx/include/span

Removed: 
    


################################################################################
diff  --git a/libcxx/include/span b/libcxx/include/span
index 41100c91edfba..023e62778414b 100644
--- a/libcxx/include/span
+++ b/libcxx/include/span
@@ -199,6 +199,15 @@ concept __span_compatible_range =
   is_convertible_v<remove_reference_t<ranges::range_reference_t<_Range>>(*)[], _ElementType(*)[]>;
 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
 
+template <class _From, class _To>
+concept __span_array_convertible = is_convertible_v<_From(*)[], _To(*)[]>;
+
+template <class _It, class _Tp>
+concept __span_compatible_iterator = contiguous_iterator<_It> && __span_array_convertible<remove_reference_t<iter_reference_t<_It>>, _Tp>;
+
+template <class _Sentinel, class _It>
+concept __span_compatible_sentinel_for = sized_sentinel_for<_Sentinel, _It> && !is_convertible_v<_Sentinel, size_t>;
+
 #if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_ABI_SPAN_POINTER_ITERATORS)
 #   define _LIBCPP_SPAN_USE_POINTER_ITERATOR
 #endif
@@ -225,16 +234,13 @@ public:
     static constexpr size_type extent = _Extent;
 
 // [span.cons], span constructors, copy, assignment, and destructor
-    template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
+    template <size_t _Sz = _Extent> requires(_Sz == 0)
     _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
 
     constexpr span           (const span&) noexcept = default;
     constexpr span& operator=(const span&) noexcept = default;
 
-    template <class _It,
-              enable_if_t<contiguous_iterator<_It> &&
-                              is_convertible_v<remove_reference_t<iter_reference_t<_It>>(*)[], element_type (*)[]>,
-                          nullptr_t> = nullptr>
+    template <__span_compatible_iterator<element_type> _It>
     _LIBCPP_INLINE_VISIBILITY
     constexpr explicit span(_It __first, size_type __count)
         : __data{_VSTD::to_address(__first)} {
@@ -242,11 +248,7 @@ public:
       _LIBCPP_ASSERT(_Extent == __count, "size mismatch in span's constructor (iterator, len)");
     }
 
-    template <
-        class _It, class _End,
-        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
-                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
-                    nullptr_t> = nullptr>
+    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
     _LIBCPP_INLINE_VISIBILITY
     constexpr explicit span(_It __first, _End __last) : __data{_VSTD::to_address(__first)} {
       (void)__last;
@@ -257,13 +259,12 @@ public:
 
     _LIBCPP_INLINE_VISIBILITY constexpr span(type_identity_t<element_type> (&__arr)[_Extent]) noexcept : __data{__arr} {}
 
-    template <class _OtherElementType,
-              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
+    template <__span_array_convertible<element_type> _OtherElementType>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
 
-    template <class _OtherElementType,
-              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
+    template <class _OtherElementType>
+        requires __span_array_convertible<const _OtherElementType, element_type>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(const array<_OtherElementType, _Extent>& __arr) noexcept : __data{__arr.data()} {}
 
@@ -288,20 +289,14 @@ public:
     }
 #endif
 
-    template <class _OtherElementType>
+    template <__span_array_convertible<element_type> _OtherElementType>
     _LIBCPP_INLINE_VISIBILITY
-        constexpr span(const span<_OtherElementType, _Extent>& __other,
-                       enable_if_t<
-                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
-                          nullptr_t> = nullptr)
+        constexpr span(const span<_OtherElementType, _Extent>& __other)
         : __data{__other.data()} {}
 
-    template <class _OtherElementType>
+    template <__span_array_convertible<element_type> _OtherElementType>
     _LIBCPP_INLINE_VISIBILITY
-        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other,
-                       enable_if_t<
-                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
-                          nullptr_t> = nullptr) noexcept
+        constexpr explicit span(const span<_OtherElementType, dynamic_extent>& __other) noexcept
         : __data{__other.data()} { _LIBCPP_ASSERT(_Extent == __other.size(), "size mismatch in span's constructor (other span)"); }
 
 
@@ -412,14 +407,11 @@ public:
 
 private:
     pointer    __data;
-
 };
 
 
 template <typename _Tp>
 class _LIBCPP_TEMPLATE_VIS span<_Tp, dynamic_extent> {
-private:
-
 public:
 //  constants and types
     using element_type           = _Tp;
@@ -445,19 +437,12 @@ public:
     constexpr span           (const span&) noexcept = default;
     constexpr span& operator=(const span&) noexcept = default;
 
-    template <class _It,
-              enable_if_t<contiguous_iterator<_It> &&
-                              is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]>,
-                          nullptr_t> = nullptr>
+    template <__span_compatible_iterator<element_type> _It>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(_It __first, size_type __count)
         : __data{_VSTD::to_address(__first)}, __size{__count} {}
 
-    template <
-        class _It, class _End,
-        enable_if_t<is_convertible_v<remove_reference_t<iter_reference_t<_It> > (*)[], element_type (*)[]> &&
-                        contiguous_iterator<_It> && sized_sentinel_for<_End, _It> && !is_convertible_v<_End, size_t>,
-                    nullptr_t> = nullptr>
+    template <__span_compatible_iterator<element_type> _It, __span_compatible_sentinel_for<_It> _End>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(_It __first, _End __last)
         : __data(_VSTD::to_address(__first)), __size(__last - __first) {}
@@ -466,13 +451,12 @@ public:
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(type_identity_t<element_type> (&__arr)[_Sz]) noexcept : __data{__arr}, __size{_Sz} {}
 
-    template <class _OtherElementType, size_t _Sz,
-              enable_if_t<is_convertible_v<_OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
+    template <__span_array_convertible<element_type> _OtherElementType, size_t _Sz>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
 
-    template <class _OtherElementType, size_t _Sz,
-              enable_if_t<is_convertible_v<const _OtherElementType(*)[], element_type (*)[]>, nullptr_t> = nullptr>
+    template <class _OtherElementType, size_t _Sz>
+        requires __span_array_convertible<const _OtherElementType, element_type>
     _LIBCPP_INLINE_VISIBILITY
     constexpr span(const array<_OtherElementType, _Sz>& __arr) noexcept : __data{__arr.data()}, __size{_Sz} {}
 
@@ -491,12 +475,9 @@ public:
     constexpr span(_Range&& __r) : __data(ranges::data(__r)), __size{ranges::size(__r)} {}
 #endif
 
-    template <class _OtherElementType, size_t _OtherExtent>
+    template <__span_array_convertible<element_type> _OtherElementType, size_t _OtherExtent>
     _LIBCPP_INLINE_VISIBILITY
-        constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
-                       enable_if_t<
-                          is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
-                          nullptr_t> = nullptr) noexcept
+        constexpr span(const span<_OtherElementType, _OtherExtent>& __other)  noexcept
         : __data{__other.data()}, __size{__other.size()} {}
 
 //    ~span() noexcept = default;
@@ -616,13 +597,11 @@ inline constexpr bool ranges::enable_view<span<_ElementType, _Extent>> = true;
 template <class _Tp, size_t _Extent>
 _LIBCPP_INLINE_VISIBILITY
 auto as_bytes(span<_Tp, _Extent> __s) noexcept
--> decltype(__s.__as_bytes())
-{ return    __s.__as_bytes(); }
+{ return __s.__as_bytes(); }
 
-template <class _Tp, size_t _Extent>
+template <class _Tp, size_t _Extent> requires(!is_const_v<_Tp>)
 _LIBCPP_INLINE_VISIBILITY
 auto as_writable_bytes(span<_Tp, _Extent> __s) noexcept
--> enable_if_t<!is_const_v<_Tp>, decltype(__s.__as_writable_bytes())>
 { return __s.__as_writable_bytes(); }
 
 #if _LIBCPP_STD_VER > 17


        


More information about the libcxx-commits mailing list