From e94c3ffbf039a033be3e05795cbc9495749777bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 24 Jan 2024 12:00:43 +0100 Subject: [PATCH] Math: make unary Vector operator+() constexpr. No reason not to. Well, except MSVC 2015, because of course that one is a special snowflake, FFS. --- src/Magnum/Math/Test/VectorTest.cpp | 21 +++++++++++++++++---- src/Magnum/Math/Vector.h | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index 456fe6c92..1b8907911 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -400,10 +400,14 @@ void VectorTest::compareComponentWise() { } void VectorTest::promotedNegated() { - CORRADE_COMPARE(+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), - 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})); + CORRADE_COMPARE(-(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() { @@ -841,6 +845,15 @@ void VectorTest::subclass() { /* Unary operators */ 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)); /* Addition / subtraction */ diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index f4715f1bb..7feee1aef 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -345,7 +345,7 @@ template class Vector { * * Returns the value as-is. */ - Vector operator+() const { return *this; } + constexpr Vector operator+() const { return *this; } /** * @brief Negated vector @@ -1281,7 +1281,7 @@ extern template MAGNUM_EXPORT Debug& operator<<(Debug&, const Vector<4, Double>& return Math::Vector::pad(a, value); \ } \ \ - Type operator+() const { \ + constexpr Type operator+() const { \ return Math::Vector::operator+(); \ } \ template typename std::enable_if::value, Type>::type \