lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] valyuta/004 58962eb 01/11: Refactor


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/004 58962eb 01/11: Refactor
Date: Sun, 20 Dec 2020 17:40:08 -0500 (EST)

branch: valyuta/004
commit 58962eb3e6acf018e557e5b752a39cbf1e50ec44
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Refactor
    
    Write "T {}" to get a zero of type T, as the "Implementation note"
    explains, to avoid needless work when constructing "currency {}".
---
 stratified_algorithms.hpp | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/stratified_algorithms.hpp b/stratified_algorithms.hpp
index 4dd7631..a31a4d8 100644
--- a/stratified_algorithms.hpp
+++ b/stratified_algorithms.hpp
@@ -74,6 +74,15 @@
 /// so it's in the second bracket. The second rate is applied to the
 /// entire amount of 1500. The function is not monotone except in the
 /// degenerate case of uniformly equal rates.
+///
+/// Implementation note: Many function templates in this header
+/// require a zero of the appropriate type T, and accordingly define
+/// a local variable
+///   static constexpr T zero {};
+/// in case this zero is expensive to construct. It is constructed
+/// as "T{}" rather than "T(0)" because the latter uses an explicit
+/// integer argument, which may require a converting constructor
+/// (for example, with class currency).
 
 namespace tiered_and_banded_rates{} // doxygen workaround.
 
@@ -159,8 +168,7 @@ T tiered_product<T>::operator()
     ,std::vector<T> const& rates
     ) const
 {
-    // Cache T(0) in case it's expensive to construct.
-    T const zero = T(0);
+    static constexpr T zero {};
 
     LMI_ASSERT(zero <= new_incremental_amount);
     LMI_ASSERT(zero <= prior_total_amount);
@@ -225,8 +233,7 @@ T tiered_rate<T>::operator()
     ,std::vector<T> const& rates
     ) const
 {
-    // Cache T(0) in case it's expensive to construct.
-    T const zero = T(0);
+    static constexpr T zero {};
 
     T product = tiered_product<T>()(amount, zero, incremental_limits, rates);
     T result = rates.at(0);
@@ -279,8 +286,7 @@ T banded_rate<T>::operator()
     ,std::vector<T> const& rates
     ) const
 {
-    // Cache T(0) in case it's expensive to construct.
-    T const zero = T(0);
+    static constexpr T zero {};
 
     LMI_ASSERT(zero <= total_amount);
     LMI_ASSERT(!cumulative_limits.empty());
@@ -343,8 +349,7 @@ T banded_product<T>::operator()
 template<typename T>
 void progressively_limit(T& a, T& b, T const& limit)
 {
-    // Cache T(0) in case it's expensive to construct.
-    T const zero = T(0);
+    static constexpr T zero {};
 
     LMI_ASSERT(zero <= limit);
     if(a <= zero && b <= zero)
@@ -464,7 +469,7 @@ void progressively_limit(T& a, T& b, T const& limit)
 template<typename T>
 T progressively_reduce(T& a, T& b, T const& delta)
 {
-    T const zero = T(0); // Cache T(0) in case it's expensive to construct.
+    static constexpr T zero {};
     T r(delta);          // Return value.
     if(zero == r)
         {



reply via email to

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