lundi 3 août 2015

Can I force initialization for all possible enum values of an enum class class/function template?

I have a template class using an enum class as template parameter, defined in a header file:

// MyClass.h
enum class MyEnum {FOO, BAR};

template<MyEnum T>class MyClass {
  void doStuff();
  // ...
};

I would like to have the actual implementations of the member functions in a separate source file. I know, that in that case I have to enforce the initialization of the template for each case:

//MyClass.cpp
template<MyEnum T>void MyClass<T>::doStuff() {
  // ...
}
// Implementations of other functions

template<>MyClass<MyEnum::FOO>;
template<>MyClass<MyEnum::BAR>;

Typically, I know that I need the class with all possible values for the enum anyway, so I would like to tell the compiler that it should actually build the template class for each possible value without explicitly mentioning each possibility.

In other words: I want to replace the last two lines in the example by some automated code.

Is this somehow possible?

I would also like to do the same with template functions instead of a template class.

Aucun commentaire:

Enregistrer un commentaire