C++ Class Template Specialization

  • + 1 comment
    // Define specializations for the Traits class template here.
    template <>
    struct Traits<Fruit> {
        static string name(int index) {
            switch (static_cast<Fruit>(index)) {
                case Fruit::apple:
                    return "apple";
                case Fruit::orange:
                    return "orange";
                case Fruit::pear:
                    return "pear";
                default:
                    return "unknown";
            }
        }
    };
    
    // Specialization for Color
    template <>
    struct Traits<Color> {
        static string name(int index) {
            switch (static_cast<Color>(index)) {
                case Color::red:
                    return "red";
                case Color::green:
                    return "green";
                case Color::orange:
                    return "orange";
                default:
                    return "unknown";
            }
        }
    };