lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a ce


From: Greg Chicares
Subject: [lmi-commits] [lmi] master a6f2830d 01/10: Explain the necessity of a certain cast
Date: Mon, 20 Jun 2022 19:16:17 -0400 (EDT)

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

    Explain the necessity of a certain cast
    
    Without the cast, 'std::uint8_t' is promoted to 'signed int': i.e., both
    its width and its signedness change. The conditional ('?:') operator's
    third operand is likewise promoted.
    
    Assigning the 'signed int' return value to a 'std::int16_t' loop counter
    in the unit test engenders a gcc '-Wconversion' warning where 'int' is
    at least 32 bits wide. Arguably that warning is too aggressive because
    the compiler can deduce that the value is representable in 16 bits, but
    the cast makes the intention explicit.
---
 math_functions.hpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/math_functions.hpp b/math_functions.hpp
index e1eded1a..372cf74e 100644
--- a/math_functions.hpp
+++ b/math_functions.hpp
@@ -167,6 +167,12 @@ 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 cast on the last line may appear superfluous, but it is not:
+/// unary '-' performs integral promotion on its operand and returns a
+/// result of the promoted type, which is (signed) 'int' if type T is
+/// narrower than 'int'. Without this cast, gcc would issue a warning
+/// in the accompanying unit test.
 
 template<typename T>
 constexpr auto u_abs(T t)



reply via email to

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