diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index eea06cfb99ba2..b23188cbdadeb 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1759,6 +1759,12 @@ bool none_of(R &&Range, UnaryPredicate P) { return std::none_of(adl_begin(Range), adl_end(Range), P); } +/// Provide wrappers to std::fill which take ranges instead of having to pass +/// begin/end explicitly. +template void fill(R &&Range, T &&Value) { + std::fill(adl_begin(Range), adl_end(Range), std::forward(Value)); +} + /// Provide wrappers to std::find which take ranges instead of having to pass /// begin/end explicitly. template auto find(R &&Range, const T &Val) { diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index 286cfa745fd14..9fda0d912a2f5 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -1591,6 +1591,17 @@ TEST(STLExtrasTest, Includes) { } } +TEST(STLExtrasTest, Fill) { + std::vector V1 = {1, 2, 3}; + std::vector V2; + int Val = 4; + fill(V1, Val); + EXPECT_THAT(V1, ElementsAre(Val, Val, Val)); + V2.resize(4); + fill(V2, Val); + EXPECT_THAT(V2, ElementsAre(Val, Val, Val, Val)); +} + struct Foo; struct Bar {};