diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1657,6 +1657,15 @@ return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range); } +template +constexpr bool is_contained(std::initializer_list Set, T Value) { + // TODO: Use std::find when we switch to C++20. + for (T V : Set) + if (V == Value) + return true; + return false; +} + /// Wrapper function around std::is_sorted to check if elements in a range \p R /// are sorted with respect to a comparator \p C. template bool is_sorted(R &&Range, Compare C) { diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -964,4 +964,29 @@ (std::is_same>::value)); } +enum Doggos { + Floofer, + Woofer, + SubWoofer, + Pupper, + Pupperino, + Longboi, +}; + +TEST(STLExtrasTest, IsContainedInitializerList) { + EXPECT_TRUE(is_contained({Woofer, SubWoofer}, Woofer)); + EXPECT_TRUE(is_contained({Woofer, SubWoofer}, SubWoofer)); + EXPECT_FALSE(is_contained({Woofer, SubWoofer}, Pupper)); + EXPECT_FALSE(is_contained({}, Longboi)); + + static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!"); + static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!"); + + EXPECT_TRUE(is_contained({1, 2, 3, 4}, 3)); + EXPECT_FALSE(is_contained({1, 2, 3, 4}, 5)); + + static_assert(is_contained({1, 2, 3, 4}, 3), "It's there!"); + static_assert(!is_contained({1, 2, 3, 4}, 5), "It's not there :("); +} + } // namespace