lundi 26 décembre 2016

Redundant syntax with `enum class`

Suppose I have a templated class, where I use an enum class as a template parameter. Here's a full (trivial) example:

enum class AnimalType { Dog, Cat, Hamster };

template <AnimalType>
class Animal {
public:
  std::string speak();
};

template <>
std::string Animal<AnimalType::Dog>::speak() {
  return "Woof";
}

template <>
std::string Animal<AnimalType::Cat>::speak() {
  return "Meow";
}

template <>
std::string Animal<AnimalType::Hamster>::speak() {
  return "Iä! Iä! Cthulhu fhtagn!";
}

int main() {
  Animal<AnimalType::Dog> dog;
  Animal<AnimalType::Cat> cat;
  Animal<AnimalType::Hamster> hamster;

  cout << dog.speak() << endl << cat.speak() << endl << hamster.speak() << endl;
}

Is there a way of avoiding the verbose and redundant Animal<AnimalType::Hamster> syntax?

Aucun commentaire:

Enregistrer un commentaire