Search code examples
c++qtclang-static-analyzer

clang: Use QHash<K,T> instead of QMap<K,T> when K is a pointer


There is a warning

Use QHash<K,T> instead of QMap<K,T> when K is a pointer [clazy-qmap-with-pointer-key]

produced by clang. But I couldn't google out an explanation on why QHash is preferable... (In my case I have less than 100 pointers). Are there any clarifications about that warning?


Solution

  • You are using Clazy, that is a tool (actually a compiler plugin) that helps clang to "diggest" qt semantics better when compiling...

    clazy has some rules, and the one you are violating is the Level 0:

    qmap-with-pointer-key

    they document the reason as following:

    QMap has the particularity of sorting it's keys, but sorting by memory address makes no sense. Use QHash instead, which provides faster lookups.