Browse Source

Math: test & document why Deg/Rad needs a "copy constructor".

pull/638/head
Vladimír Vondruš 2 years ago
parent
commit
e670d14585
  1. 4
      src/Magnum/Math/Angle.h
  2. 11
      src/Magnum/Math/Test/AngleTest.cpp

4
src/Magnum/Math/Angle.h

@ -124,6 +124,8 @@ template<class T> class Deg: public Unit<Deg, T> {
template<class U> constexpr explicit Deg(Unit<Math::Deg, U> value) noexcept: Unit<Math::Deg, T>(value) {}
/** @brief Copy constructor */
/* Needed in order to make arithmetic operations (which have a Unit
return type) convertible to Deg */
constexpr /*implicit*/ Deg(Unit<Math::Deg, T> other) noexcept: Unit<Math::Deg, T>(other) {}
/**
@ -211,6 +213,8 @@ template<class T> class Rad: public Unit<Rad, T> {
template<class U> constexpr explicit Rad(Unit<Math::Rad, U> value) noexcept: Unit<Math::Rad, T>(value) {}
/** @brief Copy constructor */
/* Needed in order to make arithmetic operations (which have a Unit
return type) convertible to Rad */
constexpr /*implicit*/ Rad(Unit<Math::Rad, T> value) noexcept: Unit<Math::Rad, T>(value) {}
/**

11
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<Rad>::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<decltype(a), const Degd>::value);

Loading…
Cancel
Save