For any built-in data type, the "default constructor" form gives you the default value. So given a template like so:
template <typename Type>
void Funk()
{
Type val = Type(); // Initialize to default value.
}
This. Perfect answer. Tried it, works great! Didn't know it works with buildin ones, too. And I finally understood whats behind the int(...) cast - it's like creating a new object out of the original value, except that it's casted.
This will solve your default value problem.
As for the other problem, try using the C++ header <cmath> instead of the C header <math.h>. I think <cmath> has some additional overloads, but I may be wrong.
Nope, at a maximum only floats and doubles for both libraries. Or atleast for the C++ variant.
On your other point, I think this is perhaps a sign that what you're trying to do might not be a good idea. I'd be wary of casting back and forth between floats in the integer version since you could get precision errors, which should never happen with integers.
If you've already implemented them, what's the gain in joining them together, especially given that all the operations which are relevant for a float vector might not be relevant for an int vector?
I only use float and int for coordinates. And convert between them to get a) less details due to conversion or b) more details due to later operations. There is no case where I loose wanted details (otherwise it wouldn't the great way it works atm
). See, I have a grid in my game and every object has an int value (for rastering and faster access) and a float versions (for physics). I only the float versions for physics with no unwanted loss when converting back to int (dropping the float details is what I want at this point since each int has).
And NO, I won't discuss whether it's good or not to store grid/physics seperation like that.
More specific, this is (was!) my problem:
int Distance2D() {return int(sqrt(pow(float(x),2.0)+pow(float(y),2.0)));}
It's a method of my 3D vector class (x,y,z...), the "Int3D" variant. My idea is to convert into something like different, like:
T Distance2D() {return T(sqrt(pow(float(x),2.0)+pow(float(y),2.0)));}
or
T Distance2D() {return T(std::sqrt(pow(float(x),2.0)+pow(float(y),2.0)));}
In this way I would a) guerantee that every possible int gets in the correct float format and every resulting value will be converted back to it's right place. Before rewriting the code I only used the int version, which was my only concern. If I want to calculate the distance using integer I always have to use floats using this C method! Therefore, if I wanted a float distance for integers I would just convert them to float and then use the method. Otherwise I can make use of the int variant which won't be a problem with my program. I also guess the float(float) cast WILL be optimized by the compiler since it's obviously (and logically) superfluous.
To be honest I think you might be better off keeping them as separate types.
I kept them as seperate types before and it was more than annoying to maintain all them when adding a new feature relevant to all of them. They are so similar and I'v never used calculations which won't work for both int and float. It's the way to go. There is no reason to not using one template for this project, a birectional thing.