I try to make a template to only work with double and a user-defined type. The user can choose the user defined type by a typedef in a included header file.
typedef float Usertype;
I solved this by writing the following in the end of my template source files:
template class SpaceDiscretizer<Datatype::Usertype>;
template class SpaceDiscretizer<double>;
However I run into a problem when the user defines Usertype as double! I tried to follow some directions, namely these topics: How can I provide template specializations for typedefs of the same type?, Conditional explicit template instantiation
struct dummy{};
template class SpaceDiscretizer< std::conditional<std::is_same<double,Datatype::Usertype>::value, dummy, double>::type>;
//typedef typename std::enable_if<false,double>::type mytype;
//template class SpaceDiscretizer<mytype>;
template class SpaceDiscretizer<Datatype::Usertype>;
The first one doesn't work,because if SpaceDiscretizer gets initialized with anything non-complicated,except for double, it does not work. But I want to give the user a easy way to add a working alternative to double in the feature. The second (commented out) try fails, because this is not an overload and if type is non existent, mytype won't be ignored, but an error is put out instead.
Edit: I have a lot of places, where I don't use a template or must ensure that multiple unconnected classes use the same datatype. I am requiered to keep the implementation in .cpp file. To solve this, I include a header file to all the relevant places, so that the user can change the value at one place and have it all be consistent. A problem arises, because I always want to initialize for double, but it would be natural for the user to specify double as the Usertype.