inline int factorial(int n) { if(!n) return 1; else return n*factorial(n-1); }
As I was reading this, found that the above code would lead to ‘infinite compilation’ if not handled by compiler correctly.
How does the compiler decide whether to inline a function or not ?
First, the
inlinespecification on a function is just a hint. The compiler can (and often does) completely ignore the presence or absence of aninlinequalifier. With that said, a compiler can inline a recursive function, much as it can unroll an infinite loop. It simply has to place a limit on the level to which it will ‘unroll’ the function.An optimizing compiler might turn this code:
into this code:
In this case, we’ve basically inlined the function 3 times. Some compilers do perform this optimization. I recall MSVC++ having a setting to tune the level of inlining that would be performed on recursive functions (up to 20, I believe).