-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy path006_utility_cmp_less_equal.cpp
49 lines (44 loc) · 1.19 KB
/
006_utility_cmp_less_equal.cpp
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
template< class T, class U >
constexpr bool cmp_equal( T t, U u ) noexcept
{
using UT = std::make_unsigned_t<T>;
using UU = std::make_unsigned_t<U>;
if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
return t == u;
else if constexpr (std::is_signed_v<T>)
return t < 0 ? false : UT(t) == u;
else
return u < 0 ? false : t == UU(u);
}
template< class T, class U >
constexpr bool cmp_not_equal( T t, U u ) noexcept
{
return !cmp_equal(t, u);
}
template< class T, class U >
constexpr bool cmp_less( T t, U u ) noexcept
{
using UT = std::make_unsigned_t<T>;
using UU = std::make_unsigned_t<U>;
if constexpr (std::is_signed_v<T> == std::is_signed_v<U>)
return t < u;
else if constexpr (std::is_signed_v<T>)
return t < 0 ? true : UT(t) < u;
else
return u < 0 ? false : t < UU(u);
}
template< class T, class U >
constexpr bool cmp_greater( T t, U u ) noexcept
{
return cmp_less(u, t);
}
template< class T, class U >
constexpr bool cmp_less_equal( T t, U u ) noexcept
{
return !cmp_greater(t, u);
}
template< class T, class U >
constexpr bool cmp_greater_equal( T t, U u ) noexcept
{
return !cmp_less(t, u);
}