[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master dd32674: Show values and types when bourn_cas
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master dd32674: Show values and types when bourn_cast cannot preserve value |
Date: |
Thu, 6 Apr 2017 14:30:34 -0400 (EDT) |
branch: master
commit dd3267482cdfab4e09689fb61d26c335f0f163ca
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Show values and types when bourn_cast cannot preserve value
---
bourn_cast.hpp | 16 +++++++++++++++-
bourn_cast_test.cpp | 2 +-
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/bourn_cast.hpp b/bourn_cast.hpp
index 358952f..c0021c7 100644
--- a/bourn_cast.hpp
+++ b/bourn_cast.hpp
@@ -24,8 +24,11 @@
#include "config.hpp"
+#include "rtti_lmi.hpp" // lmi::TypeInfo [demangling]
+
#include <cmath> // isinf(), isnan(), signbit()
#include <limits>
+#include <sstream>
#include <stdexcept>
/// Numeric stinted cast, across whose bourn no value is returned.
@@ -135,7 +138,18 @@ inline To bourn_cast(From from)
throw std::runtime_error("Cast would transgress upper limit.");
To const r = static_cast<To>(from);
if(r != from)
- throw std::runtime_error("Cast would not preserve value.");
+ {
+ lmi::TypeInfo from_type(typeid(From));
+ lmi::TypeInfo to_type(typeid(To ));
+ std::ostringstream oss;
+ oss.setf(std::ios_base::fixed, std::ios_base::floatfield);
+ oss
+ << "Cast from " << from << " [" << from_type << "]"
+ << " to " << r << " [" << to_type << "]"
+ << " would not preserve value."
+ ;
+ throw std::runtime_error(oss.str());
+ }
return r;
}
diff --git a/bourn_cast_test.cpp b/bourn_cast_test.cpp
index 31a413b..a2bb52c 100644
--- a/bourn_cast_test.cpp
+++ b/bourn_cast_test.cpp
@@ -394,7 +394,7 @@ void test_conv_fpint(char const* file, int line)
BOOST_TEST_THROW
(bourn_cast<I>(F(3.14))
,std::runtime_error
- ,"Cast would not preserve value."
+ ,lmi_test::what_regex("^Cast.*would not preserve value\\.$")
);
#else // defined TEST_BOOST_CAST_INSTEAD
// boost::numeric cast truncates whereas bourn_cast throws; both
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [lmi-commits] [lmi] master dd32674: Show values and types when bourn_cast cannot preserve value,
Greg Chicares <=