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.
This commit is contained in:
Benoit Jacob 2010-03-30 14:47:45 -04:00
parent 9e0d8697c7
commit 16e416b8d7
2 changed files with 9 additions and 6 deletions

View File

@ -132,7 +132,7 @@ struct ei_significant_decimals_impl
typedef typename NumTraits<Scalar>::Real RealScalar; typedef typename NumTraits<Scalar>::Real RealScalar;
static inline int run() 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))));
} }
}; };

View File

@ -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); 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);
} }