diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index 5c7d2e0d8..23fecc55f 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -78,6 +78,11 @@ void VectorTest::multiplyDivide() { QVERIFY(vec*-1.5f == multiplied); QVERIFY(multiplied/-1.5f == vec); + + Math::Vector<1, char> vecChar(32); + Math::Vector<1, char> multipliedChar(-48); + QVERIFY(vecChar*-1.5f == multipliedChar); + QVERIFY(multipliedChar/-1.5f == vecChar); } void VectorTest::addSubstract() { diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 4440c3b29..2f34a0fa2 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -135,8 +135,14 @@ template class Vector { return !operator==(other); } - /** @brief Multiply vector */ - inline Vector operator*(T number) const { + /** + * @brief Multiply vector + * + * Note that corresponding operator with swapped type order + * (multiplying number with vector) is not available, because it would + * cause ambiguity in some cases. + */ + template inline Vector operator*(U number) const { return Vector(*this)*=number; } @@ -146,7 +152,7 @@ template class Vector { * More efficient than operator*(), because it does the computation * in-place. */ - Vector& operator*=(T number) { + template Vector& operator*=(U number) { for(size_t i = 0; i != size; ++i) (*this)[i] *= number; @@ -154,7 +160,7 @@ template class Vector { } /** @brief Divide vector */ - inline Vector operator/(T number) const { + template inline Vector operator/(U number) const { return Vector(*this)/=number; } @@ -164,7 +170,7 @@ template class Vector { * More efficient than operator/(), because it does the computation * in-place. */ - Vector& operator/=(T number) { + template Vector& operator/=(U number) { for(size_t i = 0; i != size; ++i) (*this)[i] /= number; @@ -271,17 +277,17 @@ template class Vector { return *this; \ } \ \ - inline Type operator*(T number) const { \ + template inline Type operator*(U number) const { \ return Vector::operator*(number); \ } \ - inline Type& operator*=(T number) { \ + template inline Type& operator*=(U number) { \ Vector::operator*=(number); \ return *this; \ } \ - inline Type operator/(T number) const { \ + template inline Type operator/(U number) const { \ return Vector::operator/(number); \ } \ - inline Type& operator/=(T number) { \ + template inline Type& operator/=(U number) { \ Vector::operator/=(number); \ return *this; \ } \