diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2010-03-30 14:47:45 -0400 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2010-03-30 14:47:45 -0400 |
commit | 16e416b8d745b1fc64111581fec431d843591274 (patch) | |
tree | 5f51c52e864a8d94760e1ea7457f65f519fa5844 /Eigen/src/Core | |
parent | 9e0d8697c78fb4668246c59186eca342ba3eff88 (diff) |
generalize the idea of the previous commit to all kinds of casts, see this forum thread:
http://forum.kde.org/viewtopic.php?f=74&t=86914
this is important to allow users to support custom types that don't have the needed conversion operators.
Diffstat (limited to 'Eigen/src/Core')
-rw-r--r-- | Eigen/src/Core/IO.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/MathFunctions.h | 13 |
2 files changed, 9 insertions, 6 deletions
diff --git a/Eigen/src/Core/IO.h b/Eigen/src/Core/IO.h index 22db103ed..c98742246 100644 --- a/Eigen/src/Core/IO.h +++ b/Eigen/src/Core/IO.h @@ -132,7 +132,7 @@ struct ei_significant_decimals_impl typedef typename NumTraits<Scalar>::Real RealScalar; static inline int run() { - return ei_cast_to_int(std::ceil(-ei_log(NumTraits<RealScalar>::epsilon())/ei_log(RealScalar(10)))); + return ei_cast<RealScalar,int>(std::ceil(-ei_log(NumTraits<RealScalar>::epsilon())/ei_log(RealScalar(10)))); } }; diff --git a/Eigen/src/Core/MathFunctions.h b/Eigen/src/Core/MathFunctions.h index 943b44cfa..4a21ec975 100644 --- a/Eigen/src/Core/MathFunctions.h +++ b/Eigen/src/Core/MathFunctions.h @@ -44,17 +44,20 @@ template<typename T> inline typename NumTraits<T>::Real ei_hypot(T x, T y) return p * ei_sqrt(T(1) + qp*qp); } -template<typename T> struct ei_cast_to_int_impl +// the point of wrapping these casts in this helper template struct is to allow users to specialize it to custom types +// that may not have the needed conversion operators (especially as c++98 doesn't have explicit conversion operators). + +template<typename OldType, typename NewType> struct ei_cast_impl { - static int run(const T& x) + static inline NewType run(const OldType& x) { - return int(x); + return static_cast<NewType>(x); } }; -template<typename T> inline int ei_cast_to_int(const T& x) +template<typename OldType, typename NewType> inline NewType ei_cast(const OldType& x) { - return ei_cast_to_int_impl<T>::run(x); + return ei_cast_impl<OldType, NewType>::run(x); } |