abseil / abseil/abseil-cpp

flat_hash_map: Allow non-copyable keys

Đang mở
#1,151 0 bình luận 1 reaction 0 người được giao Xem trên GitHub
question
Ngôn ngữ chính
C++
Star
18.1k
Fork
3.2k
Merge trung bình
20 giờ 36 phút
Pull request đã merge (30 ngày)
1

Mô tả

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 :)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.