Skip to content

[ADT] Allow structured binding on StringMap of move-only type #110043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rzblue
Copy link

@rzblue rzblue commented Sep 25, 2024

Currently, decltype(auto) evaluates to ValueTy rather than ValueTy&. Parenthesizing the return value fixes this, but a non-const overload needed to be added to allow use of structured binding on a non-const StringMapEntry.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 25, 2024

@llvm/pr-subscribers-llvm-adt

Author: Ryan Blue (rzblue)

Changes

Currently, decltype(auto) evaluates to ValueTy rather than ValueTy&. Parenthesizing the return value fixes this, but a non-const overload needed to be added to allow use of structured binding on a non-const StringMapEntry.


Full diff: https://github.com/llvm/llvm-project/pull/110043.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringMapEntry.h (+12-2)
  • (modified) llvm/unittests/ADT/StringMapTest.cpp (+10)
diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h
index 98b51cc1aebd59..2dac30d05ee092 100644
--- a/llvm/include/llvm/ADT/StringMapEntry.h
+++ b/llvm/include/llvm/ADT/StringMapEntry.h
@@ -148,14 +148,24 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
   }
 };
 
-// Allow structured bindings on StringMapEntry.
+// Allow structured bindings on const StringMapEntry.
 template <std::size_t Index, typename ValueTy>
 decltype(auto) get(const StringMapEntry<ValueTy> &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 <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 llvm
diff --git a/llvm/unittests/ADT/StringMapTest.cpp b/llvm/unittests/ADT/StringMapTest.cpp
index c9ef3f8a096ee9..b35afc6cec40ed 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<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 {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants