[PATCH] D41223: [libc++] Fix PR35491 - std::array of zero-size doesn't work with non-default constructible types.

Eric Fiselier via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 13 22:45:41 PST 2017


EricWF created this revision.
EricWF added a reviewer: mclow.lists.

This patch fixes llvm.org/PR35491

The fix attempts to maintain ABI compatibility by replacing the array with a instance of `aligned_storage`.


https://reviews.llvm.org/D41223

Files:
  include/array
  test/std/containers/sequences/array/array.cons/default.pass.cpp


Index: test/std/containers/sequences/array/array.cons/default.pass.cpp
===================================================================
--- test/std/containers/sequences/array/array.cons/default.pass.cpp
+++ test/std/containers/sequences/array/array.cons/default.pass.cpp
@@ -14,6 +14,10 @@
 #include <array>
 #include <cassert>
 
+struct NoDefault {
+  NoDefault(int) {}
+};
+
 int main()
 {
     {
@@ -28,4 +32,9 @@
         C c;
         assert(c.size() == 0);
     }
+  {
+    typedef std::array<NoDefault, 0> C;
+    C c;
+    assert(c.size() == 0);
+  }
 }
Index: include/array
===================================================================
--- include/array
+++ include/array
@@ -134,7 +134,12 @@
     typedef std::reverse_iterator<iterator>       reverse_iterator;
     typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
 
-    value_type __elems_[_Size > 0 ? _Size : 1];
+    typedef typename conditional<_Size == 0,
+        typename aligned_storage<sizeof(_Tp), alignment_of<_Tp>::value>::type,
+        value_type[_Size == 0 ? 1 : _Size]
+      >::type _StorageType;
+
+    _StorageType __elems_;
 
     // No explicit construct/copy/destroy for aggregate type
     _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41223.126896.patch
Type: text/x-patch
Size: 1264 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171214/ee53c079/attachment.bin>


More information about the cfe-commits mailing list