lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 9d0a95d8 3/5: Fix defect introduced 20220602T


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 9d0a95d8 3/5: Fix defect introduced 20220602T2137Z: incorrect comment
Date: Sat, 11 Jun 2022 16:52:45 -0400 (EDT)

branch: master
commit 9d0a95d8f71844894c63e6a808494d4fcf3cecde
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Fix defect introduced 20220602T2137Z: incorrect comment
    
    Commit 4ef9a22264c69 of 20220602T2137Z claimed that gcc would deduce
    a return type incorrectly, but gcc is correct. If T is 'short int',
    then the types on the original line are as follows:
    
    -    return (t < 0) ? -static_cast<U>(t) : static_cast<U>(t);
                          ^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^
                                  int          unsigned short int
    
    because unary '-' performs integral promotion.
    
    '-Warith-conversion' would diagnose this with a clearer message than
    the one quoted in commit 4ef9a22264c69.
---
 math_functions.hpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/math_functions.hpp b/math_functions.hpp
index a2a7c208..fa09c375 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -134,19 +134,17 @@ T signum(T t)
 /// Asserts that both integer types have no padding, to rule out the
 ///   UINT_MAX == INT_MAX == -(INT_MIN+1)
 /// case that Daniel Fischer points out somewhere on the web.
-///
-/// The return type is specified explicitly because with 'auto'
-/// gcc deduces it incorrectly in the accompanying unit test.
 
 template<typename T>
-constexpr std::make_unsigned_t<T> u_abs(T t)
+constexpr auto u_abs(T t)
 {
     static_assert(std::is_integral_v<T>);
     static_assert(std::is_signed_v<T>);
     using U = std::make_unsigned_t<T>;
     static_assert(std::has_unique_object_representations_v<T>);
     static_assert(std::has_unique_object_representations_v<U>);
-    return (t < 0) ? -static_cast<U>(t) : static_cast<U>(t);
+    U const u {static_cast<U>(t)};
+    return (t < 0) ? static_cast<U>(-u) : u;
 }
 
 // Actuarial functions.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]