- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
// Приведение численного типа к структуре с битовыми полями
template <class STRUCT_T, typename T>
STRUCT_T struct_cast(const T n)
{
static_assert(std::is_integral<T>::value, "Integral type required as T");
static_assert(std::is_class<STRUCT_T>::value, "class or struct type required as STRUCT_T");
static_assert(sizeof(T) == sizeof(STRUCT_T), "Incompatible types passed");
return *(reinterpret_cast<const STRUCT_T*>(&n));
}
// Приведение структур с битовыми полями к численному типу
template <typename T, class STRUCT_T>
T integral_cast(const STRUCT_T& s)
{
static_assert(std::is_integral<T>::value, "Integral type required as T");
static_assert(std::is_class<STRUCT_T>::value, "class or struct type required as STRUCT_T");
static_assert(sizeof(T) == sizeof(STRUCT_T), "Incompatible types passed");
return *(reinterpret_cast<const T*>(&s));
}