[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 137e545 1/2: Avoid type conversion
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 137e545 1/2: Avoid type conversion |
Date: |
Sat, 16 Jan 2021 04:55:53 -0500 (EST) |
branch: master
commit 137e545fcec649ba62b336b1aa965918df7a2855
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Avoid type conversion
---
stratified_algorithms.hpp | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
diff --git a/stratified_algorithms.hpp b/stratified_algorithms.hpp
index ea868a8..3270473 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)
{