From e670d14585f1792d18c47394b581b370ba392f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 11 Feb 2024 21:11:29 +0100 Subject: [PATCH] Math: test & document why Deg/Rad needs a "copy constructor". --- src/Magnum/Math/Angle.h | 4 ++++ src/Magnum/Math/Test/AngleTest.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Magnum/Math/Angle.h b/src/Magnum/Math/Angle.h index 416a91300..1a16ba6bb 100644 --- a/src/Magnum/Math/Angle.h +++ b/src/Magnum/Math/Angle.h @@ -124,6 +124,8 @@ template class Deg: public Unit { template constexpr explicit Deg(Unit value) noexcept: Unit(value) {} /** @brief Copy constructor */ + /* Needed in order to make arithmetic operations (which have a Unit + return type) convertible to Deg */ constexpr /*implicit*/ Deg(Unit other) noexcept: Unit(other) {} /** @@ -211,6 +213,8 @@ template class Rad: public Unit { template constexpr explicit Rad(Unit value) noexcept: Unit(value) {} /** @brief Copy constructor */ + /* Needed in order to make arithmetic operations (which have a Unit + return type) convertible to Rad */ constexpr /*implicit*/ Rad(Unit value) noexcept: Unit(value) {} /** diff --git a/src/Magnum/Math/Test/AngleTest.cpp b/src/Magnum/Math/Test/AngleTest.cpp index 8d6502210..5d4c9f181 100644 --- a/src/Magnum/Math/Test/AngleTest.cpp +++ b/src/Magnum/Math/Test/AngleTest.cpp @@ -46,6 +46,7 @@ struct AngleTest: TestSuite::Tester { void constructNoInit(); void constructConversion(); void constructCopy(); + void constructFromBase(); void literals(); void conversion(); @@ -137,6 +138,7 @@ AngleTest::AngleTest() { &AngleTest::constructNoInit, &AngleTest::constructConversion, &AngleTest::constructCopy, + &AngleTest::constructFromBase, &AngleTest::literals, &AngleTest::conversion, @@ -300,6 +302,15 @@ void AngleTest::constructCopy() { CORRADE_VERIFY(std::is_nothrow_copy_assignable::value); } +void AngleTest::constructFromBase() { + /* The operation returns Unit instead of the leaf type, so this can work + only if the base class has a "copy constructor" from the base type */ + Deg a = 35.0_degf + 0.15_degf; + Radd b = 1.0_rad + 0.25_rad; + CORRADE_COMPARE(a, 35.15_degf); + CORRADE_COMPARE(b, 1.25_rad); +} + void AngleTest::literals() { constexpr auto a = 25.0_deg; CORRADE_VERIFY(std::is_same::value);