lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] odd/eraseme_limits 5e7ba88 6/6: Specialize std::nume


From: Greg Chicares
Subject: [lmi-commits] [lmi] odd/eraseme_limits 5e7ba88 6/6: Specialize std::numeric_limits, sort of
Date: Sat, 6 Mar 2021 17:57:42 -0500 (EST)

branch: odd/eraseme_limits
commit 5e7ba882828c5e7906d99dab2633782caa7ed83b
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Specialize std::numeric_limits, sort of
    
    Although it's assuredly possible to specialize std::numeric_limits for
    a UDT, it seems that almost nobody does.
    
    Perhaps this exercise shows that it's not generally advisable.
    
    The original motivation was merely to assert this:
        static_assert(std::numeric_limits<T>::is_bounded);
    before using max() and lowest(), to ensure that they'd do something
    "meaningful" according to the Standard. That assertion is okay except
    for 'input_realization.cpp', which instantiates class minmax for
    Trammeled Numeric Ranges. Perhaps it should instead use class tn_range's
    bounteous range-checking facilities directly--that's probably easier
    than injecting them into std::numeric_limits.
---
 miscellany.hpp |  2 ++
 tn_range.hpp   | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/miscellany.hpp b/miscellany.hpp
index cb803a7..4179fad 100644
--- a/miscellany.hpp
+++ b/miscellany.hpp
@@ -94,6 +94,8 @@ std::string floating_rep(T t)
 template<typename T>
 class minmax
 {
+    static_assert(std::numeric_limits<T>::is_bounded);
+
   public:
     minmax()
         :minimum_ {std::numeric_limits<T>::max()}
diff --git a/tn_range.hpp b/tn_range.hpp
index d0d84c5..23f89cf 100644
--- a/tn_range.hpp
+++ b/tn_range.hpp
@@ -26,6 +26,7 @@
 
 #include "datum_base.hpp"
 
+#include <limits>
 #include <string>
 #include <type_traits>
 #include <typeinfo>
@@ -232,6 +233,7 @@ class tn_range final
     static_assert(std::is_base_of_v<trammel_base<Number>,Trammel>);
 
     friend class tn_range_test;
+//  template<typename> friend class std::numeric_limits;
 
     typedef Number number_type;
     typedef Trammel trammel_type;
@@ -320,4 +322,20 @@ bool operator!=(std::string const& s, 
tn_range<Number,Trammel> const& z)
     return !z.operator==(s);
 }
 
+namespace std
+{
+template<typename Number, typename Trammel>
+struct numeric_limits<tn_range<Number,Trammel>>
+{
+//  static auto lowest() {return trammel_type::nominal_minimum();}
+//  static auto lowest() {return Trammel::nominal_minimum();}
+//  static auto max()    {return tn_range<Number,Trammel>::maximum();}
+//  static auto max()    {return Trammel::nominal_maximum();}
+    // This is crude, but has the virtue of actually compiling:
+    static auto lowest() {return std::numeric_limits<Number>::lowest();}
+    static auto max()    {return std::numeric_limits<Number>::max();}
+    static constexpr bool is_bounded = true;
+};
+} // namespace std
+
 #endif // tn_range_hpp



reply via email to

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