C++ Class Template Specialization

Sort by

recency

|

124 Discussions

|

  • + 0 comments

    include

    using namespace std; enum class Fruit { apple, orange, pear }; enum class Color { red, green, orange };

    template struct Traits;

    template <> struct Traits { static string name(int index) { switch (index) { case 0: return "red"; case 1: return "green"; case 2: return "orange"; default: return "unknown"; } } };

    template <> struct Traits { static string name(int index ) { switch (index) { case 0: return "apple"; case 1: return "orange"; case 2: return "pear"; default: return "unknown"; } } };

    int main() { int t = 0; std::cin >> t;

    for (int i=0; i!=t; ++i) {
        int index1; std::cin >> index1;
        int index2; std::cin >> index2;
        cout << Traits<Color>::name(index1) << " ";
        cout << Traits<Fruit>::name(index2) << "\n";
    }
    

    }

  • + 0 comments

    // We can represent enums like numbers

    template <>
    struct Traits<Color> {
        static string name(int index) {
            switch (index) {
                case 0: return "red";
                case 1: return "green";
                case 2: return "orange";
                default: return "unknown";
            }
        }
    };
    
    // Specialization for Fruit
    template <>
    struct Traits<Fruit> {
        static string name(int index ) {
            switch (index) {
                case 0: return "apple";
                case 1: return "orange";
                case 2: return "pear";
                default: return "unknown";
            }
        }
    };
    
  • + 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";
            }
        }
    };
    
  • + 0 comments
    template<class T>
    struct Traits
    {
        static std::string name(int);
    };
    
    template<>
    std::string Traits<Color>::name(int n)
    {
        if(n==(int)Color::green)return std::string("green");
        if(n==(int)Color::red)return std::string("red");   
        if(n==(int)Color::orange)return std::string("orange");
        return std::string("unknown");
    };
    
    template <>
    std::string Traits<Fruit>::name(int n)
    {
        if(n==(int)Fruit::apple)return std::string("apple");
        if(n==(int)Fruit::orange)return std::string("orange");   
        if(n==(int)Fruit::pear)return std::string("pear");
        return std::string("unknown");   
    }
    
  • + 0 comments

    i thing it like that rule

    #include <iostream>
    using namespace std;
    enum class Fruit { apple, orange, pear };
    enum class Color { red, green, orange };
    
    template <typename T> struct Traits;
    
    // Define specializations for the Traits class template here.
    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";
            }
        }
    };
    
    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";
            }
        }
    };
    
    
    int main()
    {
    	int t = 0; std::cin >> t;
    
        for (int i=0; i!=t; ++i) {
            int index1; std::cin >> index1;
            int index2; std::cin >> index2;
            cout << Traits<Color>::name(index1) << " ";
            cout << Traits<Fruit>::name(index2) << "\n";
        }
    }