In this HackerRank C++ Class Template Specialization problem in c++ You are given the main function that reads the enumeration values for two different types as input, then prints out the corresponding enumeration names. Write a class template that can provide the names of the enumeration values for both types. If the enumeration value is not valid, then print unknown.

HackerRank C++ Class Template Specialization problem solution

 HackerRank C++ Class Template Specialization problem solution

// Define specializations for the Traits class template here.
template <>
struct Traits<Fruit>
{
public:
    static const char* name(int index) {
        Fruit f = (Fruit)index;
        switch (f)
        {
            case Fruit::apple:  return "apple";
            case Fruit::orange: return "orange";
            case Fruit::pear:   return "pear";
        }
        return "unknown";
    }
};
    
template <>
struct Traits<Color>
{
public:
    static const char* name(int index) {
        Color c = (Color)index;
        switch (c)
        {
            case Color::red:    return "red";
            case Color::green:  return "green";
            case Color::orange: return "orange";
        }
        return "unknown";
    }
};