...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdexcept>
int fact(int i) noexcept(false) {
if (i < 0) {
// Negative factorials are undefined.
throw std::domain_error("i must be >= 0");
}
// Use the lazy-initialized cache.
static int cache[17];
if (i < (sizeof(cache) / sizeof(int))) {
if (0 == cache[i]) {
cache[i] = i > 0 ? i * fact(i - 1) : 1;
}
return cache[i];
}
return i > 0 ? i * fact(i - 1) : 1;
}
|
...