[upstream_utils] StringMap: fix structured bindings with move-only types (#7127)

This commit is contained in:
Ryan Blue
2024-09-25 01:11:41 -04:00
committed by GitHub
parent 69af7785f6
commit b8ff3fcee2
40 changed files with 112 additions and 38 deletions

View File

@@ -159,7 +159,17 @@ decltype(auto) get(const StringMapEntry<ValueTy> &E) {
if constexpr (Index == 0)
return E.first();
else
return E.second;
return (E.second);
}
// Allow structured bindings on StringMapEntry.
template <std::size_t Index, typename ValueTy>
decltype(auto) get(StringMapEntry<ValueTy> &E) {
static_assert(Index < 2);
if constexpr (Index == 0)
return E.first();
else
return (E.second);
}
} // end namespace wpi

View File

@@ -525,6 +525,16 @@ TEST_F(StringMapTest, StructuredBindings) {
}
}
TEST_F(StringMapTest, StructuredBindingsMoveOnly) {
StringMap<MoveOnly> 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 {