[PATCH] D147426: [SmallVector] Add an explicit SmallVector(size_t Size) constructor.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 2 23:55:44 PDT 2023


craig.topper created this revision.
craig.topper added a reviewer: dblaikie.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added a project: LLVM.

Previously we use the SmallVector(size_t Size, const T& Value) constructor
with a default constructed Value. This will copy construct every element
in the vector, but not all types can be copy constructed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147426

Files:
  llvm/include/llvm/ADT/SmallVector.h
  llvm/unittests/ADT/SmallVectorTest.cpp


Index: llvm/unittests/ADT/SmallVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/SmallVectorTest.cpp
+++ llvm/unittests/ADT/SmallVectorTest.cpp
@@ -171,6 +171,10 @@
   V.resize(42);
 }
 
+LLVM_ATTRIBUTE_USED void ConstructNonCopyable() {
+  SmallVector<NonCopyable, 0> V(42);
+}
+
 // Assert that v contains the specified values, in order.
 template <typename VectorT>
 void assertValuesInOrder(VectorT &v, size_t size, ...) {
Index: llvm/include/llvm/ADT/SmallVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallVector.h
+++ llvm/include/llvm/ADT/SmallVector.h
@@ -1206,7 +1206,12 @@
     this->destroy_range(this->begin(), this->end());
   }
 
-  explicit SmallVector(size_t Size, const T &Value = T())
+  explicit SmallVector(size_t Size)
+    : SmallVectorImpl<T>(N) {
+    this->resize(Size);
+  }
+
+  SmallVector(size_t Size, const T &Value)
     : SmallVectorImpl<T>(N) {
     this->assign(Size, Value);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147426.510409.patch
Type: text/x-patch
Size: 1029 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230403/7dbf255c/attachment.bin>


More information about the llvm-commits mailing list