From 475dd6f626ee2679578ed570e9fb78f7e957a36d Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Mon, 3 Apr 2023 13:22:36 -0700 Subject: [PATCH] [SmallVector] Add an explicit SmallVector(size_t Size) constructor. 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 | 7 ++++++- llvm/unittests/ADT/SmallVectorTest.cpp | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h index 98dce89..93d9491 100644 --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -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(N) { + this->resize(Size); + } + + SmallVector(size_t Size, const T &Value) : SmallVectorImpl(N) { this->assign(Size, Value); } diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp index fd3780a..8465257 100644 --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -171,6 +171,11 @@ LLVM_ATTRIBUTE_USED void CompileTest() { V.resize(42); } +TEST(SmallVectorTest, ConstructNonCopyableTest) { + SmallVector V(42); + EXPECT_EQ(V.size(), 42); +} + // Assert that v contains the specified values, in order. template void assertValuesInOrder(VectorT &v, size_t size, ...) { -- 2.7.4