I'm trying to create 2D vectors which coordinates are generic(Vector2Dint, Vector2Dfloat, etc...) to be able to do some math operations easier. I've seen this question is pretty close to mine: Can't operator == be applied to generic types in C#? but my case is with "*" operator. Basically I need to make some math functions like "cross" or "dot" for this vectors, one example is this one:
public static T cross<T>(Vec2<T> u, Vec2<T> v)
{
return u.x * v.y - u.y * v.x;
}
The thing is Visual Studio tells me the same: "Operator '*' cannot be applied to operands of type 'T' and 'T' " (T is the type of the coordinate). My thought was to overload the ' *' operator inside the class "Vec2" to be able to multiply these coordinates, having this:
public static Vec2<T> operator *(T x, T y)
{
return x * y;
}
But Visual Studio again tells me the same thing again. I'm sure I'm not overloading operators in the right way but I don't know the reason. Thanks for the help
EDIT 1: Ok so what we can learn from this is, if you want to define a function for diferent types (int, float, double, etc...) and you want performance, the best way to do it is defining the function for each type, but now, another problem comes up for me. If you have a very long function or several long functions, ¿is this still being the best way?