In my code, I implement a template of Vector3 like below:
template <typename T>
struct TVector3{
TVector3(const T& x, const T& y, const T& z); // normal constructor from x,y,z
TVector3(const T& val); // construct from a constant value
// .. other implementation
T x, y, z;
};
// and my overload operator+ for TVector3
template <typename T>
const TVector3<T> operator+(const TVector3<T>& lhs, const TVector3<T>& rhs)
{
return TVector3<T>(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
}
I'm expecting call like this: (1, 2, 3) + 1 = (2, 3, 4), so I wrote it like this:
TVector3<float> v(1, 2, 3);
v + 1.0f; // error, on operator "+" matches ... operand types are: TVector3<float> + float
Then I seem to understand that template deduction should completely match argument types instead of converting them, so is there any way for me to tell compiler to convert T to TVector3? I don't want to write 2 overloads like below because code would become large:
template <typename T>
const TVector3<T> operator+(const TVector3<T>& lhs, const T& rhs);
template <typename T>
const TVector3<T> operator+(const T& rhs, const TVector3<T>& rhs);
Thanks for helping!