vendredi 18 septembre 2020

C++ Enum Template Functions: Compile Time Reflection

in principle the question is about the best way of getting reflexive iteration over an enum at compile time.

Let's assume I have an enum

enum Animal {
    DOG = 0,
    CAT = 12,
};

Now I have a function template

template <Animal A>
void animalVoice();

And I specialise the template:

template <>
void animalVoice<DOG>() {cout << "Bau" << endl;}
template <>
void animalVoice<CAT>() {cout << "Meow" << endl;}

Do I have the possibility of getting the same behaviour as in

int main() {
    animalVoice<DOG>;
    animalVoice<CAT>;
    return 0;
}

By iterating at compile time over the value of the enum?

Thanks

M.





Aucun commentaire:

Enregistrer un commentaire