Allow sign_cast to same type

This commit is contained in:
Danny Robson 2012-06-13 15:46:44 +10:00
parent befb6f4b2b
commit 6ff8405c6a
2 changed files with 16 additions and 4 deletions

View File

@ -17,17 +17,23 @@
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __TYPES_HPP
#define __TYPES_HPP
#ifndef __UTIL_TYPES_HPP
#define __UTIL_TYPES_HPP
#include "platform.hpp"
#include <cstdint>
#include <cstdlib>
#include <memory>
/// Returns the number of elements of a statically allocated array
#if !defined(COMPILER_CLANG)
template <typename T, size_t N>
size_t elems(T (&)[N])
{ return N; }
#else
#define elems(x) (sizeof(x) / 0[x])
#endif
template<class T, class...Args>
@ -37,4 +43,4 @@ make_unique(Args&&... args) {
}
#endif // __TYPES_HPP
#endif

View File

@ -41,13 +41,19 @@ namespace detail {
template <typename T, typename V>
T
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
_sign_cast (const typename std::enable_if<sizeof(T) == sizeof(V) &&
std::is_signed<T>::value &&
std::is_unsigned<V>::value, V>::type v)
{
CHECK_HARD (v < std::numeric_limits<V>::max () / 2);
return static_cast<T> (v);
}
template <typename T, typename V>
T
_sign_cast (const typename std::enable_if<std::is_same<T, V>::value, V>::type v)
{ return v; }
}
/// Safely cast a numeric type to its (un)signed counterpart, aborting if the