C++11 type to enum mapping?

后端 未结 3 2072
渐次进展
渐次进展 2020-12-20 21:49

I have an enum like:

enum E
{
    TYPE_FLOAT,
    TYPE_CHAR,
    TYPE_INT
}

And I want to create a compile-time mapping to get the appropri

3条回答
  •  旧巷少年郎
    2020-12-20 22:26

    Maybe because you forgot to put a semicolon after the enum definition, this works for me in LiveWorkSpace:

    #include 
    
    enum E
    {
       TYPE_FLOAT,
       TYPE_CHAR,
       TYPE_INT
    } ;
    
    template struct GetE;
    
    template<> struct GetE { static constexpr E type = TYPE_FLOAT; };
    template<> struct GetE { static constexpr E type = TYPE_CHAR; };
    template<> struct GetE { static constexpr E type = TYPE_INT; };
    
    int main()
    {
        std::cout << GetE::type << std::endl ;
    }
    

    here is a link to the code http://liveworkspace.org/code/nHqUe$6

提交回复
热议问题