src/typeconversions.h

changeset 250
2837b549e616
parent 218
63125c36de73
equal deleted inserted replaced
249:37d3c819cafa 250:2837b549e616
83 using valuetype = std::underlying_type_t<std::remove_cvref_t<T>>; 83 using valuetype = std::underlying_type_t<std::remove_cvref_t<T>>;
84 using reftype = transfer_cvref_t<T&&, valuetype>; 84 using reftype = transfer_cvref_t<T&&, valuetype>;
85 return reinterpret_cast<reftype>(enu); 85 return reinterpret_cast<reftype>(enu);
86 } 86 }
87 87
88 //! \brief A version of static_cast that only works if casting from a wider
89 //! integer type to a narrower integer type.
90 template<typename T, typename R>
91 constexpr auto narrow(R x)
92 -> std::enable_if_t<sizeof(T) <= sizeof(R) and std::is_signed_v<T> == std::is_signed_v<R>, T>
93 {
94 return static_cast<T>(x);
95 }
96
97 template<typename T, typename R>
98 constexpr auto widen(R x)
99 -> std::enable_if_t<sizeof(T) >= sizeof(R) and std::is_signed_v<T> == std::is_signed_v<R>, T>
100 {
101 return static_cast<T>(x);
102 }
103
88 #endif // TYPECONVERSIONS_H 104 #endif // TYPECONVERSIONS_H

mercurial