Here is a small piece of code:
class myClass { public: void myMethod() const { for (const auto& entity : myList) { auto iter = myMap.find(&entity); } } private: std::list<int> myList; std::unordered_map<int*,int> myMap; }; The method myMethod() is marked const since it should not change any data member.
The code does not compile, with a:
note: candidate function not viable: 1st argument ('const int *') would lose const qualifier
note: candidate function not viable: 'this' argument has type 'const std::unordered_map<int *, int>', but method is not marked const
note: candidate template ignored: requirement '__is_transparent_v<std::hash<int *>, const int *>' was not satisfied [with _K2 = const int *]
note: candidate template ignored: requirement '__is_transparent_v<std::hash<int *>, const int *>' was not satisfied [with _K2 = const int *]
Changing the std::unordered_map with a std::map does not compile either. Only a const_cast on &entity as auto iter = myMap.find(const_cast<int*>(&entity)); makes it compile.
Why do I need use a const_cast?