abseil / abseil/abseil-cpp

flat_hash_map: Allow non-copyable keys

Abierto
#1,151 0 comentarios 1 reacción 0 asignados Ver en GitHub
question
Lenguaje dominante
C++
Estrellas
18.1k
Forks
3.2k
Merge medio
20 h 36 min
PR fusionados (30 d)
1

Descripción

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

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.