diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h --- a/llvm/include/llvm/ADT/SmallVector.h +++ b/llvm/include/llvm/ADT/SmallVector.h @@ -1287,6 +1287,15 @@ return {std::begin(Range), std::end(Range)}; } +template +SmallVector to_vector_of(R &&Range) { + return {std::begin(Range), std::end(Range)}; +} + +template SmallVector to_vector_of(R &&Range) { + return {std::begin(Range), std::end(Range)}; +} + } // end namespace llvm namespace std { diff --git a/llvm/unittests/ADT/SmallVectorTest.cpp b/llvm/unittests/ADT/SmallVectorTest.cpp --- a/llvm/unittests/ADT/SmallVectorTest.cpp +++ b/llvm/unittests/ADT/SmallVectorTest.cpp @@ -203,7 +203,7 @@ } template -static unsigned NumBuiltinElts(const SmallVector &) { +constexpr static unsigned NumBuiltinElts(const SmallVector &) { return N; } @@ -1142,6 +1142,25 @@ EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); } +TEST(SmallVectorTest, ToVector) { + { + std::vector v = {'a', 'b', 'c'}; + auto Vector = to_vector<4>(v); + static_assert(NumBuiltinElts(Vector) == 4u); + ASSERT_EQ(3u, Vector.size()); + for (size_t I = 0; I < v.size(); ++I) + EXPECT_EQ(v[I], Vector[I]); + } + { + std::vector v = {'a', 'b', 'c'}; + auto Vector = to_vector(v); + static_assert(NumBuiltinElts(Vector) != 4u); + ASSERT_EQ(3u, Vector.size()); + for (size_t I = 0; I < v.size(); ++I) + EXPECT_EQ(v[I], Vector[I]); + } +} + struct To { int Content; friend bool operator==(const To &LHS, const To &RHS) { @@ -1180,6 +1199,26 @@ } } +TEST(SmallVectorTest, ToVectorOf) { + To to1{1}, to2{2}, to3{3}; + std::vector StdVector = {From(to1), From(to2), From(to3)}; + { + llvm::SmallVector Vector = llvm::to_vector_of(StdVector); + + ASSERT_EQ(StdVector.size(), Vector.size()); + for (size_t I = 0; I < StdVector.size(); ++I) + EXPECT_EQ(StdVector[I], Vector[I]); + } + { + auto Vector = llvm::to_vector_of(StdVector); + + ASSERT_EQ(StdVector.size(), Vector.size()); + static_assert(NumBuiltinElts(Vector) == 4u); + for (size_t I = 0; I < StdVector.size(); ++I) + EXPECT_EQ(StdVector[I], Vector[I]); + } +} + template class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase { protected: @@ -1476,11 +1515,6 @@ VectorT V; - template - static unsigned NumBuiltinElts(const SmallVector &) { - return N; - } - void SetUp() override { SmallVectorTestBase::SetUp();