How should i handle multiple datatype possibilities in my code?
I want it to be possible to compile using double or float type, here is the code i use at the moment:
#define USE_FLOAT_PRECISION
...
#ifdef USE_FLOAT_PRECISION
typedef float DATATYPE;
#define GL_DATATYPE GL_FLOAT
#else
typedef double DATATYPE;
#define GL_DATATYPE GL_DOUBLE
#endif
...
DATATYPE somevar;
...
for(...){
for(...){
...
somevar *= (DATATYPE)1.02; // is this good?
...
}
}
...
glVertexPointer(3, GL_DATATYPE, ... // can this be done better?
...
This works just fine, but i feel there is something bad with casting by (DATATYPE) for every place i use it, also looks ugly too, it gets annoying to paste that for every place. Any other solution?
Edit: the reason im concerned about the casting to (DATATYPE) is because i need to express the float value with double precision in my code, but then convert it to (float) later, so im afraid converting from double to float would cause some problems. Also im not sure if its efficient, ive heard that static_cast is faster or something. But im not sure why should i use it and should i use it here at all.
Instead of pasting this code in everywhere, consider putting it in a header that you can just include where you need it.
Your approach above will work and is one of several approaches to achieve what you want.
Other approaches might involve using templates which may simplify your code a little and provide greater flexibility.