Consider a function like the below:
unsigned int fact(unsigned int i) {
if (i <= 1) { return 1; }
return i * fact(i-1);
}
If I were to instantiate a new variable unsigned int f
such that f = 0 * fact(5)
, why does it not "short circuit"?
unsigned int fact(unsigned int i) {
std::cout << "a";
if (i <= 1) { return 1; }
return i * fact(i-1);
}
int main() {
unsigned int f = 0 * fact(5);
}
The output here is aaaaa
. If f
can only ever be zero, why would it call the function at all, supposing it knew the return type? Does it not evaluate left to right, see 0 * (unsigned int)
and know the rvalue will be 0
?