-
Notifications
You must be signed in to change notification settings - Fork 0
/
literals.hpp
50 lines (37 loc) · 1008 Bytes
/
literals.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
45
46
47
48
49
#pragma once
// std
#include <limits>
#include <type_traits>
#include "exception_builder.hpp"
namespace util {
template <
typename T,
typename = std::enable_if_t<std::is_integral_v<T>>
>
T check_and_cast(unsigned long long int i)
{
if ( i < std::numeric_limits<T>::lowest() || std::numeric_limits<T>::max() < i )
exception_builder<std::runtime_error>() << "argument is outside of valid range: " << i;
return (T)i;
}
}
inline uint8_t operator ""_u8(unsigned long long int i)
{
return util::check_and_cast<uint8_t>(i);
}
inline uint16_t operator ""_u16(unsigned long long int i)
{
return util::check_and_cast<uint16_t>(i);
}
inline uint32_t operator ""_u32(unsigned long long int i)
{
return util::check_and_cast<uint32_t>(i);
}
inline uint64_t operator ""_u64(unsigned long long int i)
{
return util::check_and_cast<uint64_t>(i);
}
inline size_t operator""_sz(unsigned long long int i)
{
return (size_t)i;
}