29 #include <type_traits>
49 template<
typename TType,
typename SType>
51 using stripped_source_t =
typename std::remove_reference<SType>::type;
52 using stripped_target_t =
typename std::remove_reference<TType>::type;
54 static_assert(std::is_integral<stripped_target_t>::value &&
55 std::is_signed<stripped_target_t>::value,
56 "target type must be a signed integral type");
57 static_assert(std::is_integral<stripped_source_t>::value &&
58 std::is_unsigned<stripped_source_t>::value,
59 "source type must be an unsigned integral type");
61 using target_limits = std::numeric_limits<stripped_target_t>;
64 if(source_value <=
static_cast<stripped_source_t
>((target_limits::max)())) {
66 #pragma clang diagnostic push
67 #pragma clang diagnostic ignored "-Wsign-conversion"
69 return static_cast<TType
>(std::forward<SType>(source_value));
71 #pragma clang diagnostic pop
74 throw std::range_error(
"unsigned source value is too large for signed type");
78 template<
typename TType,
typename SType>
80 using stripped_source_t =
typename std::remove_reference<SType>::type;
81 using stripped_target_t =
typename std::remove_reference<TType>::type;
83 static_assert(std::is_integral<stripped_source_t>::value &&
84 std::is_signed<stripped_source_t>::value,
85 "source type must be an unsigned integral type");
86 static_assert(std::is_integral<stripped_target_t>::value &&
87 std::is_unsigned<stripped_target_t>::value,
88 "target type must be a signed integral type");
90 using target_limits = std::numeric_limits<stripped_target_t>;
91 if(source_value < 0) {
92 throw std::range_error(
"input value is < 0 -> cannot be converted to unsigned");
95 #pragma clang diagnostic push
96 #pragma clang diagnostic ignored "-Wsign-compare"
97 #pragma clang diagnostic ignored "-Wsign-conversion"
99 if(
static_cast<stripped_target_t
>(source_value) <= (target_limits::max)()) {
100 return static_cast<stripped_target_t
>(std::forward<SType>(source_value));
102 #pragma clang diagnostic pop
105 throw std::range_error(
"signed input value does not fit into the requested unsigned target type");
TType unsigned2signed(SType &&source_value)
convert unsigned to signed integers
Definition: utilities.hpp:50
TType signed2unsigned(SType &&source_value)
Definition: utilities.hpp:79