I'm trying to hold a polymorphic type as a key in a map.
I came up with the following two structures:
Note that Game
is an abstract class and the data structure I use is :
std::unordered_map<gamePtr,int> _allGames;
while gamePtr
is a typedef
for:
unique_ptr<Game>
template<>
struct std::hash<std::unique_ptr<Game>> {
size_t operator()(std::unique_ptr<Game> game) const {
return (std::hash<string>()(std::to_string(game->firstTeamFinalScore()) + game->firstTeam() + game->secondTeam()));
}
};
struct cmp_games {
bool operator() (std::unique_ptr<Game> game1, std::unique_ptr<Game> game2) const {
return *game1 == *game2;
}
};
The cmp_games
comparator seems to work fine but the std::hash
does not because it tries to copy a unique_ptr
(Which is ofc impossible) and I've no idea how to get over it.
Would love to hear some suggestions (If that is even possible).
EDIT: The comparator also doesn't seem to work properly. how do I make this map work correctly with unique_ptr
as a key?
EDIT2:
Came up with:
template<>
struct std::hash<std::unique_ptr<Game>> {
size_t operator()(const std::unique_ptr<Game>& game) const {
return (std::hash<string>()(std::to_string(game->firstTeamFinalScore()) + game->firstTeam() + game->secondTeam()));
}
};
template<>
struct std::equal_to<std::unique_ptr<Game>> {
bool operator() (const std::unique_ptr<Game>& game1,const std::unique_ptr<Game>& game2) const {
return *game1 == *game2;
}
};
Should they be enough?
The standard provides a specilization so that
std::hash<unique_ptr<T>>
is the same asstd::hash<T*>
. So provide a specialization forstd::hash<Game *>
. For example:Live demo
Another option is to change your existing
std::hash
specialization so that it takes theunique_ptr
by reference.EDIT:
std::unique_ptr
provides comparison operators that compare the managed pointers. If you want theunordered_map
to test theGame
objects themselves for equality, provide anoperator==
overload instead of specializingstd::equal_to
This, in turn, requires that you've provided an equality operator for
Game
(or you could just add the logic to the function above).