Can inline functions have a recursion
Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function. However, inlining a function is just a request or suggestion to the compiler, not any mandatory command. It depends on the compiler whether to accept or decline this suggestion of inlining a particular function. The compiler is most likely to not consider the inlining of a function under certain circumstances that are mentioned here as follows-.
Here, the inline function add int a, int b has been defined before the main function and later been called inside the main function. When the add function is called, the code of the add function definition is placed there, i.
Henceforth, it saves from the overhead of function calls and returns. The working of the inline function add a,b has been illustrated below:. However, an inline function can be defined inside a class as well.
At the time of compilation, this inline function max a,b in the above example works in the same way as the inline function add a,b in the previous example. Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search. As I was reading this , found that the above code would lead to "infinite compilation" if not handled by compiler correctly.
First, the inline specification on a function is just a hint. The compiler can and often does completely ignore the presence or absence of an inline qualifier. 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. In this case, we've basically inlined the function 3 times.
Some compilers do perform this optimization. Indeed, if your compiler does not act intelligently, it may try inserting copies of your inline d function recursively, creating infinitely-large code. Most modern compilers will recognize this, however. They can either:.
For case 2, many compilers have pragma s you can set to specify the maximum depth to which this should be done. In gcc , you can also pass this in from the command-line with --max-inline-insns-recursive see more info here. Your function however is not tail recursive. As a "footnote", you could achieve the effect you're looking for at least for the factorial you're using as an example using template metaprogramming.
Pasting from Wikipedia:. Some recursive functions can be transformed into loops, which effectively infinitely inlines them. I believe gcc can do this, but I don't know about other compilers.
The compiler will make a call graph to detect these sorts of things and prevent them. So it would see that the function calls itself and not inline. But mainly it is controlled by the inline keyword and compiler switches For example, you can have it auto inline small functions even without the keyword.
Its important to note that Debug compilations should never be inlining as the callstack will not be preserved to mirror the calls you created in code. That depends on the compiler, the options that were specified, the version number of the compiler, maybe how much memory is available, etc. The program's source code still has to obey the rules for inlined functions. Inline expansion minimizes the function-call overhead at the potential cost of larger code size. Using inline functions can make your program faster because they eliminate the overhead associated with function calls.
Functions expanded inline are subject to code optimizations not available to normal functions. The compiler treats the inline expansion options and keywords as suggestions. There's no guarantee that functions will be inlined. The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function instantiate and create standard calling linkages instead of inserting the code inline.
Two cases where this behavior can happen are:. These reasons may interfere with inlining, as may others , at the discretion of the compiler; you shouldn't depend on the inline specifier to cause a function to be inlined. As with normal functions, there's no defined order for argument evaluation in an inline function.
In fact, it could be different from the argument evaluation order when passed using the normal function-call protocol. A class's member functions can be declared inline, either by using the inline keyword or by placing the function definition within the class definition. The compiler can't inline a function if:. With the pragma, recursive functions are inlined to a default depth of 16 calls. The function is virtual and is called virtually. Direct calls to virtual functions can be inlined. The program takes the address of the function and the call is made via the pointer to the function.
Direct calls to functions that have had their address taken can be inlined. The function is defined externally, in an included library or another translation unit, or is a virtual call target or indirect call target.
The compiler can't identify non-inlined code that it can't find in the current translation unit. After that depth, recursive function calls are treated as calls to an instance of the function.
0コメント