Skip to content

Commit e5714f4

Browse files
committed
Add some more compile time safety
1 parent bc38b65 commit e5714f4

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

value.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Value {
3434
result_t<T, U>>;
3535

3636
template <typename T>
37-
static Value returnInt(const T& x) {
37+
static Value returnInt(T x) {
3838
if (std::numeric_limits<int64_t>::min() <= x &&
3939
x <= std::numeric_limits<int64_t>::max()) {
4040
return Value{(int64_t)x};
@@ -44,7 +44,8 @@ class Value {
4444
integer result = (result_upper << 64) + result_lower;
4545
return Value{result};
4646
} else {
47-
return Value{(integer)x};
47+
static_assert(std::is_same_v<T, integer>);
48+
return Value{std::move(x)};
4849
}
4950
}
5051
template <typename T, typename U>
@@ -54,8 +55,9 @@ class Value {
5455
throw std::invalid_argument("value not suitable for pow");
5556
}
5657
integer result = pow((integer)(a), (unsigned)b);
57-
return returnInt(result);
58+
return returnInt(std::move(result));
5859
} else {
60+
static_assert(std::is_same_v<T, double>);
5961
return Value{pow(a, (double)b)};
6062
}
6163
}
@@ -119,12 +121,11 @@ class Value {
119121
std::to_string(op)};
120122
}
121123
if constexpr (is_integral<i_t>) {
122-
return returnInt(result);
124+
return returnInt(std::move(result));
123125
} else {
124126
return Value{result};
125127
}
126128
}
127-
return Value{};
128129
},
129130
value_, o.value_);
130131
}
@@ -148,7 +149,7 @@ class Value {
148149
}
149150
throw std::invalid_argument(
150151
absl::StrCat("Value (", typeid(U).name(), ")", str(),
151-
" not suitable for loop counter"));
152+
" not suitable for ", typeid(T).name()));
152153
},
153154
value_);
154155
}
@@ -171,11 +172,12 @@ class Value {
171172
"insufficient long double precision");
172173
return (long double)a < (long double)b;
173174
} else if constexpr (std::is_same_v<T, double>) {
175+
static_assert(std::is_same_v<U, integer>);
174176
return (boost::multiprecision::mpq_rational)a < b;
175-
} else if constexpr (std::is_same_v<U, double>) {
176-
return a < (boost::multiprecision::mpq_rational)b;
177177
} else {
178-
throw std::logic_error("programmer error");
178+
static_assert(std::is_same_v<U, double>);
179+
static_assert(std::is_same_v<T, integer>);
180+
return a < (boost::multiprecision::mpq_rational)b;
179181
}
180182
}
181183
},

0 commit comments

Comments
 (0)