cppassist icon indicating copy to clipboard operation
cppassist copied to clipboard

Provide generic hasher for enum classes

Open scheibel opened this issue 8 years ago • 4 comments

Possible implementation:

template<typename EnumClass>
struct EnumClassHash
{
    std::hash<unsigned char>::result_type operator()(
        const EnumClass & arg) const
    {
        static std::hash<unsigned char> hasher;
        return hasher(static_cast<unsigned char>(arg));
    }
};

Possible usage:

template <>
struct hash<MyEnumClass> : EnumClassHash<MyEnumClass> {};

scheibel avatar Dec 20 '16 09:12 scheibel

I think we'll need a new submodule name for such a class (and many to follow). It's a explicit extension to standard library concepts with the contexts hash and enum class. Any ideas? @sbusch42 @cgcostume

scheibel avatar Jan 20 '17 16:01 scheibel

Seems like C++14 and 17 added support for this with their respective stdlibs. However, as we develop cppassist using C++11 I think we have to provide fallback implementations for older compilers and stdlibs. We will have to find code to detect whether hashing is already provided by the stdlib.

scheibel avatar Feb 02 '17 11:02 scheibel

is this required? and we should think about switching to C++14?

cgcostume avatar Oct 23 '18 03:10 cgcostume

I think an upgrade to C++14 becomes feasible, soon. Fortunately, such an upgrade would imply the removal of some more implementations in cppassist.

scheibel avatar Oct 23 '18 07:10 scheibel