diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index 27d82916503e37..b5fc6d595f863c 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -16,9 +16,8 @@ #ifndef LLVM_ADT_STRINGMAPENTRY_H #define LLVM_ADT_STRINGMAPENTRY_H -#include "llvm/ADT/None.h" -#include "llvm/ADT/STLFunctionalExtras.h" #include "llvm/ADT/StringRef.h" +#include namespace llvm { @@ -149,6 +148,26 @@ class StringMapEntry final : public StringMapEntryStorage { } }; +// Allow structured bindings on StringMapEntry. +template +decltype(auto) get(const StringMapEntry &E) { + static_assert(Index < 2); + if constexpr (Index == 0) + return E.first(); + else + return E.second; +} + } // end namespace llvm +namespace std { +template +struct tuple_size> + : std::integral_constant {}; + +template +struct tuple_element> + : std::conditional {}; +} // namespace std + #endif // LLVM_ADT_STRINGMAPENTRY_H diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index e5dae25c440833..415d7f6c31f522 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -517,6 +517,16 @@ TEST_F(StringMapTest, MoveDtor) { ASSERT_TRUE(B.empty()); } +TEST_F(StringMapTest, StructuredBindings) { + StringMap A; + A["a"] = 42; + + for (auto &[Key, Value] : A) { + EXPECT_EQ("a", Key); + EXPECT_EQ(42, Value); + } +} + namespace { // Simple class that counts how many moves and copy happens when growing a map struct CountCtorCopyAndMove {