abseil / abseil/abseil-cpp

flat_hash_map: Allow non-copyable keys

未關閉
#1,151 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視
question
主要語言
C++
星號
18.1k
分支
3.2k
平均合併
20 小時 36 分鐘
30 天內合併 PR
1

描述

Hi,

It appears that `absl::flat_hash_map` does not support non-copyable keys, even though `std::unordered_map` and `absl::node_hash_map` do.

I created an example on godbolt to demonstrate the issue: https://godbolt.org/z/7GEKoa8s7. If you replace `absl::flat_hash_map` with `std::unordered_map` or `absl::node_hash_map`, the code compiles successfully.

When I dig into the compiler error in the stacktrace, it seems the relevant failure occurs here: https://github.com/abseil/abseil-cpp/blob/4dcae40a07e30cbc99d4130e590047000e5fda0b/absl/container/internal/container_memory.h#L422

I think what's happening is that `kMutableKeys::value` is true (meaning we can move `std::pair` to rehash the table, instead of copying `std::pair`, but since the if statement evaluated at "runtime", the compiler must compile both branches of the code, and the `std::pair` branch fails to compile. On my own machine, I was able to get this to compile by changing the branch to an `if constexpr`.

I'm curious if the abseil team would accept a patch to change the branches over `kMutableKeys` to compile-time branches, either using `if constexpr` or through a more backwards-compatible technique.

Thanks!
-Manu

---

For context, I would prefer to use `absl::flat_hash_map, Value>` over `absl::node_hash_map`, because the key is not cheap to hash/compare, and so I want to identify keys using bare pointers in the remainder of my program, which I can then search inside the hashtable for. If there are other ways to achieve the same use-case, would be happy to hear :)

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。