Discussion Locked

C++ Question

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Can an inline function call itself?

9 replies

by macbouy

Yes, if you have a Microsoft compiler, use:
#pragma inline_recursion( [{on | off}] )

Recursive functions usually aren't candidate for inline function protocol.

by wayne m.

Yes and No. You can use the inline syntax to declare the function, but it will probably be compiled as a normal subroutine because the code cannot be infinitely unwrapped. "inline" is only a hint to the compiler and it can decide not to inline code if need be.

Loops are usually more appropriate than recursive calls in inline functions, and loops will be actually compiled as true inline code.

by dachev

Your compiler will probably accept the recursive call. But the inline function is actually a macro and is not called with a 'call' instruction as all other functions, but is just inserted in the code. So, the compiler may just make this a usual function and do the recursion via 'call'. The compiler can create a loop instead and save the macro from becoming a function (and generate some extra code to save all variables in the stack), but this is implementation dependend. Anyway the first case you get recursion and no inline function, the second - inline function and no recursion (but this second case it has actualy the same results like the recursion). The compiler will probably be able to do it both ways and will have some option (perhaps in the optimization options) wich determines which way it will choose.

by vickydicky

Yes Inline functions can call itself, becuz prefixing inline the declaration is just request to the compiler that if it is possible to u(compiler) pleaes make this function as inline.This(inline) function is same as the normal function.
But what's the use of this(recursive) with Inline function. Inline function are used to increase the execution time.When u make such request using keyword in declaration compiler will try to put the code wherever it is called so that it can save the time. Sothat by requesting Inline to recursive function means ur requesting to increasing the size of ur programm, which ultimately slowdown ur programme.
So it is better not use inline function if u want that function calls itself.(i.e. recursive)

This discussion is closed to new replies.