Browse Source

Math: make unary Vector operator+() constexpr.

No reason not to. Well, except MSVC 2015, because of course that one is
a special snowflake, FFS.
pull/168/head
Vladimír Vondruš 2 years ago
parent
commit
e94c3ffbf0
  1. 21
      src/Magnum/Math/Test/VectorTest.cpp
  2. 4
      src/Magnum/Math/Vector.h

21
src/Magnum/Math/Test/VectorTest.cpp

@ -400,10 +400,14 @@ void VectorTest::compareComponentWise() {
} }
void VectorTest::promotedNegated() { void VectorTest::promotedNegated() {
CORRADE_COMPARE(+Vector4(1.0f, -3.0f, 5.0f, -10.0f), CORRADE_COMPARE(+(Vector4{1.0f, -3.0f, 5.0f, -10.0f}),
Vector4(1.0f, -3.0f, 5.0f, -10.0f)); (Vector4{1.0f, -3.0f, 5.0f, -10.0f}));
CORRADE_COMPARE(-Vector4(1.0f, -3.0f, 5.0f, -10.0f), CORRADE_COMPARE(-(Vector4{1.0f, -3.0f, 5.0f, -10.0f}),
Vector4(-1.0f, 3.0f, -5.0f, 10.0f)); (Vector4{-1.0f, 3.0f, -5.0f, 10.0f}));
constexpr Vector4 a{1.0f, -3.0f, 5.0f, -10.0f};
constexpr Vector4 promotedA = +a;
CORRADE_COMPARE(promotedA, a);
} }
void VectorTest::addSubtract() { void VectorTest::addSubtract() {
@ -841,6 +845,15 @@ void VectorTest::subclass() {
/* Unary operators */ /* Unary operators */
CORRADE_COMPARE(+Vec2(-2.0f, 5.0f), Vec2(-2.0f, 5.0f)); CORRADE_COMPARE(+Vec2(-2.0f, 5.0f), Vec2(-2.0f, 5.0f));
{
constexpr Vec2 ca{-2.0f, 5.0f};
#ifndef CORRADE_MSVC2015_COMPATIBILITY /* Probably because copy is not constexpr */
constexpr
#endif
Vec2 cb = +ca;
CORRADE_COMPARE(cb, ca);
}
CORRADE_COMPARE(-Vec2(-2.0f, 5.0f), Vec2(2.0f, -5.0f)); CORRADE_COMPARE(-Vec2(-2.0f, 5.0f), Vec2(2.0f, -5.0f));
/* Addition / subtraction */ /* Addition / subtraction */

4
src/Magnum/Math/Vector.h

@ -345,7 +345,7 @@ template<std::size_t size, class T> class Vector {
* *
* Returns the value as-is. * Returns the value as-is.
*/ */
Vector<size, T> operator+() const { return *this; } constexpr Vector<size, T> operator+() const { return *this; }
/** /**
* @brief Negated vector * @brief Negated vector
@ -1281,7 +1281,7 @@ extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Vector<4, Double>&
return Math::Vector<size, T>::pad(a, value); \ return Math::Vector<size, T>::pad(a, value); \
} \ } \
\ \
Type<T> operator+() const { \ constexpr Type<T> operator+() const { \
return Math::Vector<size, T>::operator+(); \ return Math::Vector<size, T>::operator+(); \
} \ } \
template<class U = T> typename std::enable_if<std::is_signed<U>::value, Type<T>>::type \ template<class U = T> typename std::enable_if<std::is_signed<U>::value, Type<T>>::type \

Loading…
Cancel
Save