Skip to content

Commit 7613b1b

Browse files
Addressed review comments
1 parent e342b2b commit 7613b1b

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/lib/support/SafeInt.h

+20-18
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ namespace chip {
3737
template <typename T, typename U, std::enable_if_t<std::is_integral<T>::value, int> = 0>
3838
bool CanCastTo(U arg)
3939
{
40+
using namespace std;
4041
// U might be a reference to an integer type, if we're assigning from
4142
// something passed by reference.
42-
typedef typename std::remove_reference<U>::type V; // V for "value"
43-
static_assert(std::is_integral<V>::value, "Must be assigning from an integral type");
43+
typedef typename remove_reference<U>::type V; // V for "value"
44+
static_assert(is_integral<V>::value, "Must be assigning from an integral type");
4445

4546
// We want to check that "arg" can fit inside T but without doing any tests
4647
// that are always true or always false due to the types involved, which
@@ -63,44 +64,44 @@ bool CanCastTo(U arg)
6364
// of T and V is signed and the other is unsigned: there might not be a
6465
// single integer type that can represent _both_ the value of arg and the
6566
// minimal/maximal value.
66-
if (std::numeric_limits<T>::is_signed && std::numeric_limits<V>::is_signed)
67+
if (numeric_limits<T>::is_signed && numeric_limits<V>::is_signed)
6768
{
68-
if (static_cast<intmax_t>(std::numeric_limits<V>::max()) <= static_cast<intmax_t>(std::numeric_limits<T>::max()) &&
69-
static_cast<intmax_t>(std::numeric_limits<V>::min()) >= static_cast<intmax_t>(std::numeric_limits<T>::min()))
69+
if (static_cast<intmax_t>(numeric_limits<V>::max()) <= static_cast<intmax_t>(numeric_limits<T>::max()) &&
70+
static_cast<intmax_t>(numeric_limits<V>::min()) >= static_cast<intmax_t>(numeric_limits<T>::min()))
7071
{
7172
// Any checks on arg would be trivially true; don't even do them, to
7273
// avoid warnings.
7374
return true;
7475
}
7576

76-
return static_cast<intmax_t>(std::numeric_limits<T>::min()) <= static_cast<intmax_t>(arg) &&
77-
static_cast<intmax_t>(arg) <= static_cast<intmax_t>(std::numeric_limits<T>::max());
77+
return static_cast<intmax_t>(numeric_limits<T>::min()) <= static_cast<intmax_t>(arg) &&
78+
static_cast<intmax_t>(arg) <= static_cast<intmax_t>(numeric_limits<T>::max());
7879
}
7980

80-
if (!std::numeric_limits<T>::is_signed && !std::numeric_limits<V>::is_signed)
81+
if (!numeric_limits<T>::is_signed && !numeric_limits<V>::is_signed)
8182
{
82-
if (static_cast<uintmax_t>(std::numeric_limits<V>::max()) <= static_cast<uintmax_t>(std::numeric_limits<T>::max()))
83+
if (static_cast<uintmax_t>(numeric_limits<V>::max()) <= static_cast<uintmax_t>(numeric_limits<T>::max()))
8384
{
8485
// Any checks on arg would be trivially true; don't even do them, to
8586
// avoid warnings.
8687
return true;
8788
}
8889

89-
return static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(std::numeric_limits<T>::max());
90+
return static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(numeric_limits<T>::max());
9091
}
9192

92-
if (std::numeric_limits<T>::is_signed)
93+
if (numeric_limits<T>::is_signed)
9394
{
94-
static_assert(std::numeric_limits<T>::max() >= 0, "What weird type is this?");
95-
if (static_cast<uintmax_t>(std::numeric_limits<V>::max()) <= static_cast<uintmax_t>(std::numeric_limits<T>::max()))
95+
static_assert(numeric_limits<T>::max() >= 0, "What weird type is this?");
96+
if (static_cast<uintmax_t>(numeric_limits<V>::max()) <= static_cast<uintmax_t>(numeric_limits<T>::max()))
9697
{
9798
return true;
9899
}
99100

100-
return static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(std::numeric_limits<T>::max());
101+
return static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(numeric_limits<T>::max());
101102
}
102103

103-
return 0 <= arg && static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(std::numeric_limits<T>::max());
104+
return 0 <= arg && static_cast<uintmax_t>(arg) <= static_cast<uintmax_t>(numeric_limits<T>::max());
104105
}
105106

106107
template <typename T, typename U, std::enable_if_t<std::is_enum<T>::value, int> = 0>
@@ -126,9 +127,10 @@ bool CanCastTo(U arg)
126127
template <typename T>
127128
typename std::enable_if<std::is_unsigned<T>::value, typename std::make_signed<T>::type>::type CastToSigned(T arg)
128129
{
129-
typedef typename std::make_signed<T>::type signed_type;
130+
using namespace std;
131+
typedef typename make_signed<T>::type signed_type;
130132

131-
if (arg <= static_cast<T>(std::numeric_limits<signed_type>::max()))
133+
if (arg <= static_cast<T>(numeric_limits<signed_type>::max()))
132134
{
133135
return static_cast<signed_type>(arg);
134136
}
@@ -140,7 +142,7 @@ typename std::enable_if<std::is_unsigned<T>::value, typename std::make_signed<T>
140142
//
141143
// then noting that both (numeric_limits<T>::max() - arg) and its negation
142144
// are guaranteed to fit in signed_type.
143-
signed_type diff = static_cast<signed_type>(std::numeric_limits<T>::max() - arg);
145+
signed_type diff = static_cast<signed_type>(numeric_limits<T>::max() - arg);
144146
return static_cast<signed_type>(-diff - 1);
145147
}
146148

src/lib/support/TypeTraits.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ namespace chip {
3434

3535
#if __cplusplus >= 202300L
3636

37-
template <class T>
38-
constexpr auto to_underlying(T e)
39-
{
40-
return std::to_underlying(e);
41-
}
37+
using to_underlying = std::to_underlying;
4238

4339
#else
4440
/**

0 commit comments

Comments
 (0)