From 50e21015eb7de91bdad923d30806544b4f57fc67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 22 Feb 2020 23:01:02 +0100 Subject: [PATCH] Math: make it possible to construct Half from a double. A lot of generic code uses T(1.0) and right now that caused an ambiguity between the Float and UnsignedShort constructors. --- src/Magnum/Math/Half.h | 11 ++++++++++- src/Magnum/Math/Test/HalfTest.cpp | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Math/Half.h b/src/Magnum/Math/Half.h index b131a4270..bddc9acd8 100644 --- a/src/Magnum/Math/Half.h +++ b/src/Magnum/Math/Half.h @@ -86,12 +86,21 @@ class Half { constexpr explicit Half(UnsignedShort data) noexcept: _data{data} {} /** - * @brief Construct a half value from 32-bit float representation + * @brief Construct a half value from a 32-bit float representation * * @see @ref packHalf() */ explicit Half(Float value) noexcept: _data{packHalf(value)} {} + /** + * @brief Construct a half value from a 64-bit float representation + * + * Present only to aid generic code so e.g. @cpp T(1.0) @ce works + * without being ambigous. + * @see @ref packHalf() + */ + explicit Half(Double value) noexcept: _data{packHalf(Float(value))} {} + /** @brief Construct without initializing the contents */ explicit Half(NoInitT) noexcept {} diff --git a/src/Magnum/Math/Test/HalfTest.cpp b/src/Magnum/Math/Test/HalfTest.cpp index 6e00b4558..3a27747a4 100644 --- a/src/Magnum/Math/Test/HalfTest.cpp +++ b/src/Magnum/Math/Test/HalfTest.cpp @@ -513,14 +513,20 @@ void HalfTest::constructDefault() { void HalfTest::constructValue() { Half a{3.5f}; + Half b{3.5}; CORRADE_COMPARE(Float(a), 3.5f); + CORRADE_COMPARE(Float(b), 3.5f); CORRADE_COMPARE(UnsignedShort(a), 0x4300); + CORRADE_COMPARE(UnsignedShort(b), 0x4300); CORRADE_COMPARE(a.data(), 0x4300); + CORRADE_COMPARE(b.data(), 0x4300); CORRADE_VERIFY((std::is_nothrow_constructible::value)); + CORRADE_VERIFY((std::is_nothrow_constructible::value)); /* Implicit conversion is not allowed */ CORRADE_VERIFY(!(std::is_convertible::value)); + CORRADE_VERIFY(!(std::is_convertible::value)); } void HalfTest::constructData() {