From 16e416b8d745b1fc64111581fec431d843591274 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Tue, 30 Mar 2010 14:47:45 -0400 Subject: [PATCH] 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. --- Eigen/src/Core/IO.h | 2 +- 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::Real RealScalar; static inline int run() { - return ei_cast_to_int(std::ceil(-ei_log(NumTraits::epsilon())/ei_log(RealScalar(10)))); + return ei_cast(std::ceil(-ei_log(NumTraits::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 inline typename NumTraits::Real ei_hypot(T x, T y) return p * ei_sqrt(T(1) + qp*qp); } -template 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 struct ei_cast_impl { - static int run(const T& x) + static inline NewType run(const OldType& x) { - return int(x); + return static_cast(x); } }; -template inline int ei_cast_to_int(const T& x) +template inline NewType ei_cast(const OldType& x) { - return ei_cast_to_int_impl::run(x); + return ei_cast_impl::run(x); }