Saturday, August 16, 2014

Enumeration Class Hash

In order to use the STL unordered map class, a hash function is required for the key of the map.  Hash functions are provided for all the built in types plus some of the other STL classes (like std::string), but unfortunately, not for enumeration classes.  For the built in types (integers), the number itself is used as the hash.  However, enumeration class enumerator values can't be because they can't be used as integers even though there values are just numbers.  The solution is to use a generic (template) function type that will work for all enumeration classes that converts the enumerators to integers:
struct EnumClassHash
{
    template <typename T>
    std::size_t operator()(T t) const
    {
        return static_cast<std::size_t>(t);
    }
};
This works for any enumeration class by returning an integer for an enumerator (a size_t is an integer that is large enough to cover any enumeration) using a static cast (hopefully the only place a cast will be needed).  An unordered map to use an enumeration class with an initializer list to assign values to the enumerators would be defined as:
std::unordered_map<EnumName, QString, EnumClassHash> names {
    {First_EnumName, "First"},
    {Second_EnumName, "Second"},
    {Third_EnumName, "Third"}
};
The name of the map does not need to be repeated for an assignment of each value (which could be tedious for a long name or with a lot of enumerators).  This does not resolve the issue of forgotten values, but hopefully when an undefined enumerator value is accessed, the default value (in this case a blank string) would be detected or identified easily.

No comments:

Post a Comment

All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)