I am using an external library to write binary data from a data store object that is constructed from a template. This is a general question so I won't mention the library here. The templates are explicitly instantiated so they can only be of either float
or double
types. I wrap the call to the library writer in my own method which needs to decide what precision to request from the library writer. This, of course, depends on which version of the class that is in use. I can't use a conditional like this:
typedef std::conditional<std::is_same<T, float>::value, MACRO_FLOAT_TYPE, MACRO_DOUBLE_TYPE>::type T1;
because I don't want to define a type I just want to define the value of precisionType
in the example below:
template <typename T>
class Data
{
// My class which can either contain data as float or double precision
// My wrapper for the library writing method which takes a custom typedef
void writeData(customType precisionType);
}
int main()
{
// Assume I need two different versions of the data store
Data<float> * dFloat = new Data<float>();
Data<double> * dDouble = new Data<double>();
// I have to call
dFloat->writeData(MACRO_TYPE_FLOAT);
dDouble->writeData(MACRO_TYPE_DOUBLE);
}
But, I want to hide this macro argument from the user as it depends on the library being used and could be different in the future.
I would like to make precisionType
a private constant member of the class that is chosen at compile-time when the templates are expanded. The user can then just call ->writeData()
and not worry about the precision type argument. How can I achieve this?