[SmallVector] Add an explicit SmallVector(size_t Size) constructor.
authorCraig Topper <craig.topper@sifive.com>
Mon, 3 Apr 2023 20:22:36 +0000 (13:22 -0700)
committerCraig Topper <craig.topper@sifive.com>
Mon, 3 Apr 2023 20:22:36 +0000 (13:22 -0700)
Previously we used the SmallVector(size_t Size, const T& Value) constructor
with a default constructed Value. That will copy construct every element
in the vector, but not all types can be copy constructed.

Reviewed By: dblaikie

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

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

index 98dce89..93d9491 100644 (file)
@@ -1206,7 +1206,12 @@ public:
     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);
   }
index fd3780a..8465257 100644 (file)
@@ -171,6 +171,11 @@ LLVM_ATTRIBUTE_USED void CompileTest() {
   V.resize(42);
 }
 
+TEST(SmallVectorTest, ConstructNonCopyableTest) {
+  SmallVector<NonCopyable, 0> V(42);
+  EXPECT_EQ(V.size(), 42);
+}
+
 // Assert that v contains the specified values, in order.
 template <typename VectorT>
 void assertValuesInOrder(VectorT &v, size_t size, ...) {