You are viewing a single comment's thread. Return to all comments →
enum class Color { red = 0, green, orange }; enum class Fruit {apple = 0, orange, pear}; template<typename T> struct EnumToString{ static string get() { return "unknown";} }; template<> struct EnumToString<Color>{ static string get(Color color) { switch(color){ case Color::red: return "red"; case Color::green: return "green"; case Color::orange: return "orange"; default: return "unknown"; } } }; template<> struct EnumToString<Fruit>{ static string get(Fruit fruit) { switch(fruit){ case Fruit::apple: return "apple"; case Fruit::orange: return "orange"; case Fruit::pear: return "pear"; default: return "unknown"; } } }; int main() { int n ; cin >> n; for(int i = 0 ; i < n ; i++){ int c, f; cin >> c >> f; Color color = Color(c); Fruit fruit = Fruit(f); cout << EnumToString<Color>::get(color) << " "; cout << EnumToString<Fruit>::get(fruit) << endl; } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
C++ Class Template Specialization
You are viewing a single comment's thread. Return to all comments →