diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index 98b51cc1aebd5..2dac30d05ee09 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -148,14 +148,24 @@ class StringMapEntry final : public StringMapEntryStorage { } }; -// Allow structured bindings on StringMapEntry. +// Allow structured bindings on const StringMapEntry. template decltype(auto) get(const StringMapEntry &E) { static_assert(Index < 2); if constexpr (Index == 0) return E.first(); else - return E.second; + return (E.second); +} + +// Allow structured bindings on StringMapEntry. +template +decltype(auto) get(StringMapEntry &E) { + static_assert(Index < 2); + if constexpr (Index == 0) + return E.first(); + else + return (E.second); } } // end namespace llvm diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp index c9ef3f8a096ee..b35afc6cec40e 100644 --- a/llvm/unittests/ADT/StringMapTest.cpp +++ b/llvm/unittests/ADT/StringMapTest.cpp @@ -552,6 +552,16 @@ TEST_F(StringMapTest, StructuredBindings) { } } +TEST_F(StringMapTest, StructuredBindingsMoveOnly) { + StringMap A; + A.insert(std::make_pair("a", MoveOnly(42))); + + for (auto &&[Key, Value] : A) { + EXPECT_EQ("a", Key); + EXPECT_EQ(42, Value.i); + } +} + namespace { // Simple class that counts how many moves and copy happens when growing a map struct CountCtorCopyAndMove {