I wanted to calculate e
value at compile–time (don't worry, not a homework), yet something went wrong.
template<size_t limit = 3, class result = std::ratio<0, 1>, size_t factorial = 1, size_t count = 1>
constexpr double e_impl() {
if constexpr(limit == 0) {
return static_cast<double>(result{}.num) / result{}.den;
}
return e_impl<limit - 1, std::ratio_add<result, std::ratio<1, factorial>>, factorial * count, count + 1>();
}
While calculated values are correct, compiler throws an error about overflow in template. It seems like limit
variable goes out of range (below 0
), but it shouldn't happen as the 0
–case is being handled by the if constexpr(…)
statement.
So the question is, am I wrong and that behavior should be expected, or is it a compiler bug? Compiled with GCC 7.1.0.