No, this is not a mistake. The problem here is that even when limit is 0 and you stop the recursion, the compiler still prints
return e_impl<limit - 1, std::ratio_add<result, std::ratio<1, factorial>>, factorial * count, count + 1>();
since it is unconditional. You need to put it in an else block to get it only for compilation, if limit not 0 .
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; else return e_impl<limit - 1, std::ratio_add<result, std::ratio<1, factorial>>, factorial * count, count + 1>(); }
source share