ircd::util: Add sign cast convenience suite.

This commit is contained in:
Jason Volk 2023-03-25 18:36:32 -07:00
parent ef930635de
commit b1c2576c20
1 changed files with 28 additions and 0 deletions

View File

@ -140,6 +140,34 @@ struct is_specialization_of<T<args...>, T>
:std::true_type
{};
//
// Convenience signedness cast template
//
template<class T>
using enable_if_s2u = std::enable_if<std::is_signed<T>::value, typename std::make_unsigned<T>::type>;
template<class T>
[[using gnu: always_inline, gnu_inline, artificial]]
extern inline typename enable_if_s2u<T>::type *
sign_cast(T *const t)
{
using type = typename std::make_unsigned<T>::type;
return reinterpret_cast<type *>(t);
}
template<class T>
using enable_if_u2s = std::enable_if<std::is_unsigned<T>::value, typename std::make_signed<T>::type>;
template<class T>
[[using gnu: always_inline, gnu_inline, artificial]]
extern inline typename enable_if_u2s<T>::type *
sign_cast(T *const t)
{
using type = typename std::make_signed<T>::type;
return reinterpret_cast<type *>(t);
}
//
// Convenience const_cast deduction template
//