[llvm] [llvm][ADT] Structured bindings for move-only types in `StringMap` (PR #114676)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 2 10:04:41 PDT 2024


================
@@ -147,25 +147,57 @@ class StringMapEntry final : public StringMapEntryStorage<ValueTy> {
 };
 
 // Allow structured bindings on StringMapEntry.
+
+namespace detail {
+template <std::size_t Index> struct StringMapEntryGet;
+
+template <> struct StringMapEntryGet<0> {
+  template <typename ValueTy> static StringRef get(StringMapEntry<ValueTy> &E) {
+    return E.getKey();
+  }
+
+  template <typename ValueTy>
+  static StringRef get(const StringMapEntry<ValueTy> &E) {
+    return E.getKey();
+  }
+};
+
+template <> struct StringMapEntryGet<1> {
+  template <typename ValueTy> static ValueTy &get(StringMapEntry<ValueTy> &E) {
+    return E.getValue();
+  }
+
+  template <typename ValueTy>
+  static const ValueTy &get(const StringMapEntry<ValueTy> &E) {
+    return E.getValue();
+  }
+};
+} // namespace detail
+
+template <std::size_t Index, typename ValueTy>
+decltype(auto) get(StringMapEntry<ValueTy> &E) {
+  return detail::StringMapEntryGet<Index>::get(E);
----------------
kuhar wrote:

Could this be an `if constexpr` instead? There are only two cases.

https://github.com/llvm/llvm-project/pull/114676


More information about the llvm-commits mailing list