-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPunning.hpp
45 lines (26 loc) · 1015 Bytes
/
Punning.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifndef PUNNING_HPP
#define PUNNING_HPP
#include <cstring>
#include <type_traits>
#include <c++/4.6/type_traits>
namespace gen {
template<typename T>
typename std::make_unsigned<T>::type
pun_s_to_u(typename std::make_signed<T>::type value) {
static_assert(std::is_integral<T>::value,
"gen::pun_s_to_u(...) - Type must be an integral type.");
typename std::make_unsigned<T>::type rv;
std::memcpy(&rv, &value, sizeof(T));
return rv;
}
template<typename T>
typename std::make_signed<T>::type
pun_u_to_s( typename std::make_unsigned<T>::type value) {
static_assert(std::is_integral<T>::value,
"gen::pun_u_to_s(...) - Type must be an integral type.");
typename std::make_signed<T>::type rv;
std::memcpy(&rv, &value, sizeof(T));
return rv;
}
}
#endif /* PUNNING_HPP */