[libcxx-commits] [libcxx] 7994108 - [libc++][span] SFINAE span default constructor on Extent == 0

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 14 06:35:23 PDT 2020


Author: Michael Schellenberger Costa
Date: 2020-05-14T09:35:07-04:00
New Revision: 79941086fba3df115e5e7b54a5f826096c933102

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

LOG: [libc++][span] SFINAE span default constructor on Extent == 0

The default constructor of a static span requires _Extent == 0 so
SFINAE it out rather than using a static_assert

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

Added: 
    

Modified: 
    libcxx/include/span
    libcxx/test/std/containers/views/span.cons/default.fail.cpp
    libcxx/test/std/containers/views/span.cons/default.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/span b/libcxx/include/span
index 21616fb0a75a..1851ff3c0465 100644
--- a/libcxx/include/span
+++ b/libcxx/include/span
@@ -200,8 +200,8 @@ public:
     static constexpr size_type extent = _Extent;
 
 // [span.cons], span constructors, copy, assignment, and destructor
-    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr}
-    { static_assert(_Extent == 0, "Can't default construct a statically sized span with size > 0"); }
+    template <size_t _Sz = _Extent, enable_if_t<_Sz == 0, nullptr_t> = nullptr>
+    _LIBCPP_INLINE_VISIBILITY constexpr span() noexcept : __data{nullptr} {}
 
     constexpr span           (const span&) noexcept = default;
     constexpr span& operator=(const span&) noexcept = default;

diff  --git a/libcxx/test/std/containers/views/span.cons/default.fail.cpp b/libcxx/test/std/containers/views/span.cons/default.fail.cpp
index 87a6e7511b7b..c9e8bd16cb0e 100644
--- a/libcxx/test/std/containers/views/span.cons/default.fail.cpp
+++ b/libcxx/test/std/containers/views/span.cons/default.fail.cpp
@@ -13,7 +13,7 @@
 // constexpr span() noexcept;
 //
 //  Remarks: This constructor shall not participate in overload resolution
-//          unless Extent <= 0 is true.
+//          unless Extent == 0 || Extent == dynamic_extent is true.
 
 
 #include <span>
@@ -24,10 +24,7 @@
 
 int main(int, char**)
 {
-    std::span<int, 2> s; // expected-error-re at span:* {{static_assert failed{{( due to requirement '.*')?}} "Can't default construct a statically sized span with size > 0"}}
-
-//  TODO: This is what I want:
-// eXpected-error {{no matching constructor for initialization of 'std::span<int, 2>'}}
+  std::span<int, 2> s; // expected-error {{no matching constructor for initialization of 'std::span<int, 2>'}}
 
   return 0;
 }

diff  --git a/libcxx/test/std/containers/views/span.cons/default.pass.cpp b/libcxx/test/std/containers/views/span.cons/default.pass.cpp
index 867026bd9aec..3899a6021f43 100644
--- a/libcxx/test/std/containers/views/span.cons/default.pass.cpp
+++ b/libcxx/test/std/containers/views/span.cons/default.pass.cpp
@@ -15,6 +15,7 @@
 #include <span>
 #include <cassert>
 #include <string>
+#include <type_traits>
 
 #include "test_macros.h"
 
@@ -79,5 +80,9 @@ int main(int, char**)
 
     checkCV();
 
-  return 0;
+    static_assert( std::is_default_constructible_v<std::span<int, std::dynamic_extent>>, "");
+    static_assert( std::is_default_constructible_v<std::span<int, 0>>, "");
+    static_assert(!std::is_default_constructible_v<std::span<int, 2>>, "");
+
+    return 0;
 }


        


More information about the libcxx-commits mailing list