How would one initialize static data members of a template class differently for particular parameters?
I understand that templates are different than other kinds of classes and only what is used in the project ever gets instantiated. Can I list a number of different initializations for different parameters and have the compiler use whichever is appropriate?
For example, does the following work, and if not what is the correct way to do this? :
template<class T>
class someClass
{
static T someData;
// other data, functions, etc...
};
template<class T>
T someClass::someData = T.getValue();
template<>
int someClass<int>::someData = 5;
template<>
double someClass<double>::someData = 5.0;
// etc...
Should work. You may need to put these into the .c file instead of the header.
Here is also a working partial template specialization with initialization of static data members: