[PATCH] D144416: [STLExtras] Relax type requirements for `is_contained`

Jakub Kuderski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 21 10:14:40 PST 2023


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG75272d5c1299: [ADT] Relax type requirements for `is_contained` (authored by kuhar).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144416/new/

https://reviews.llvm.org/D144416

Files:
  llvm/include/llvm/ADT/STLExtras.h
  llvm/unittests/ADT/STLExtrasTest.cpp


Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -947,11 +947,28 @@
   Longboi,
 };
 
+struct WooferCmp {
+  // Not copyable.
+  WooferCmp(const WooferCmp &) = delete;
+  WooferCmp &operator=(const WooferCmp &) = delete;
+
+  friend bool operator==(const Doggos &Doggo, const WooferCmp &) {
+    return Doggo == Doggos::Woofer;
+  }
+};
+
 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));
+
+  // Check that the initializer list type and the element type do not have to
+  // match exactly.
+  EXPECT_TRUE(is_contained({Floofer, Woofer, SubWoofer}, WooferCmp{}));
+  EXPECT_FALSE(is_contained({Floofer, SubWoofer}, WooferCmp{}));
+
+  EXPECT_TRUE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("ccc")));
+  EXPECT_FALSE(is_contained({"a", "bb", "ccc", "dddd"}, llvm::StringRef("x")));
 
   static_assert(is_contained({Woofer, SubWoofer}, SubWoofer), "SubWoofer!");
   static_assert(!is_contained({Woofer, SubWoofer}, Pupper), "Missing Pupper!");
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1870,11 +1870,13 @@
   return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
 }
 
-template <typename T>
-constexpr bool is_contained(std::initializer_list<T> Set, T Value) {
+/// Returns true iff \p Element exists in \p Set. This overload takes \p Set as
+/// an initializer list and is `constexpr`-friendly.
+template <typename T, typename E>
+constexpr bool is_contained(std::initializer_list<T> Set, const E &Element) {
   // TODO: Use std::find when we switch to C++20.
-  for (T V : Set)
-    if (V == Value)
+  for (const T &V : Set)
+    if (V == Element)
       return true;
   return false;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144416.499223.patch
Type: text/x-patch
Size: 2148 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230221/8cd4d80c/attachment.bin>


More information about the llvm-commits mailing list