Can't use enum class as unordered_map key

后端 未结 7 1330
后悔当初
后悔当初 2020-12-12 20:29

I have a class containing an enum class.

class Shader {
public:
    enum class Type {
        Vertex   = GL_VERTEX_SHADER,
        Geometry = GL_GEOMETRY_SHA         


        
相关标签:
7条回答
  • 2020-12-12 21:04

    As KerrekSB pointed out, you need to provide a specialization of std::hash if you want to use std::unordered_map, something like:

    namespace std
    {
        template<>
        struct hash< ::Shader::Type >
        {
            typedef ::Shader::Type argument_type;
            typedef std::underlying_type< argument_type >::type underlying_type;
            typedef std::hash< underlying_type >::result_type result_type;
            result_type operator()( const argument_type& arg ) const
            {
                std::hash< underlying_type > hasher;
                return hasher( static_cast< underlying_type >( arg ) );
            }
        };
    }
    
    0 讨论(0)
提交回复
热议问题