cast: allow trunc_cast to same type
this makes templated code slightly easier to write
This commit is contained in:
parent
e5d44d0c6d
commit
6781f7a361
55
cast.hpp
55
cast.hpp
@ -68,6 +68,7 @@ sign_cast (const U u)
|
|||||||
|
|
||||||
|
|
||||||
///----------------------------------------------------------------------------
|
///----------------------------------------------------------------------------
|
||||||
|
#if !defined(NDEBUG)
|
||||||
/// assert if the value cannot be cast loslessly from U to T, else return the
|
/// assert if the value cannot be cast loslessly from U to T, else return the
|
||||||
/// converted value.Note: this is only a debug-time check and is compiled out
|
/// converted value.Note: this is only a debug-time check and is compiled out
|
||||||
/// in optimised builds.
|
/// in optimised builds.
|
||||||
@ -76,37 +77,41 @@ sign_cast (const U u)
|
|||||||
/// to preserve NaN values.
|
/// to preserve NaN values.
|
||||||
///
|
///
|
||||||
/// allows the identity cast (if only to make cross-platform porting easier)
|
/// allows the identity cast (if only to make cross-platform porting easier)
|
||||||
template <
|
template <typename DstT, typename SrcT>
|
||||||
typename T,
|
constexpr
|
||||||
typename U
|
|
||||||
>
|
|
||||||
std::enable_if_t<
|
std::enable_if_t<
|
||||||
// only enabled for builtin types. there's no reason this couldn't be
|
std::is_arithmetic<SrcT>::value &&
|
||||||
// extended, but we've only really tested on integers and floats.
|
std::is_arithmetic<DstT>::value,
|
||||||
std::is_fundamental<T>::value && std::is_fundamental<U>::value && (
|
DstT
|
||||||
// either we're casting between real and integer
|
|
||||||
std::is_floating_point<T>::value != std::is_floating_point<U>::value
|
|
||||||
// or we're reducing the size of the type
|
|
||||||
|| sizeof (T) <= sizeof (U)
|
|
||||||
),
|
|
||||||
T
|
|
||||||
>
|
>
|
||||||
trunc_cast (const U &u)
|
trunc_cast (const SrcT src)
|
||||||
{
|
{
|
||||||
auto t = static_cast<T> (u);
|
auto dst = static_cast<DstT> (src);
|
||||||
|
|
||||||
// if we're not a NaN then check equality by casting U-to-T-to-U. we can't
|
// check if we're testing a NaN by using self-equality. this will not hold
|
||||||
// explicitly check isnan because that call isn't valid for arbitrary
|
// for NaN, but makes specialisation easier given we don't need to special
|
||||||
// types and we'll cause a compile-time error.
|
// case for integers (which don't have std::isnan).
|
||||||
if (u == u)
|
if (src == src) {
|
||||||
CHECK_EQ (static_cast<U> (t), u);
|
CHECK_EQ (static_cast<SrcT> (dst), src);
|
||||||
// we must be a NaN. test that t is a NaN too by self-equality.
|
} else {
|
||||||
else
|
CHECK_NEQ (dst, dst);
|
||||||
CHECK_NEQ (t, t);
|
|
||||||
|
|
||||||
return t;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
// in release mode we don't give a fuck what happens. cast away all safety.
|
||||||
|
template <typename DstT, typename SrcT>
|
||||||
|
DstT
|
||||||
|
trunc_cast (const SrcT src)
|
||||||
|
{
|
||||||
|
return static_cast<DstT> (src);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
/// assert if the value is not a pointer to a subclass of T, else return the
|
/// assert if the value is not a pointer to a subclass of T, else return the
|
||||||
|
Loading…
Reference in New Issue
Block a user