Discussion:
Can we make recursive funcions "Inline" ?
(too old to reply)
Sachin
2009-08-26 12:51:09 UTC
Permalink
hi
Inline is sometimes to used for optimizing a proecss.
Can we make recursive functions inline and what will be its behavior ,
what are possibilities that compiler will create inline definition i mean
does compiler treat it any special way ?

sachin
Ulrich Eckhardt
2009-08-26 13:22:11 UTC
Permalink
Post by Sachin
Inline is sometimes to used for optimizing a proecss.
Can we make recursive functions inline and what will be its behavior ,
what are possibilities that compiler will create inline definition i mean
does compiler treat it any special way ?
The only guaranteed behaviour of 'inline' is that multiple definitions in
different translation units don't cause errors.

Further, it suggests to the compiler that it should paste the generated
code, i.e. elide any function call overhead. This must not change the
semantics though. Actually, if the result is not distinguishable, the
compiler can do what it wants, like inlining code at will. Modern compilers
do exactly that, they treat 'inline' as just a suggestion.

As far as recursive functions are concerned, you can not include its code as
part of its code, so no actual inlining can take place, regardless of the
presence of the 'inline' keyword.


Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Carl Daniel [VC++ MVP]
2009-08-26 13:41:33 UTC
Permalink
Post by Ulrich Eckhardt
Post by Sachin
Inline is sometimes to used for optimizing a proecss.
Can we make recursive functions inline and what will be its behavior
, what are possibilities that compiler will create inline definition
i mean does compiler treat it any special way ?
The only guaranteed behaviour of 'inline' is that multiple
definitions in different translation units don't cause errors.
Further, it suggests to the compiler that it should paste the
generated code, i.e. elide any function call overhead. This must not
change the semantics though. Actually, if the result is not
distinguishable, the compiler can do what it wants, like inlining
code at will. Modern compilers do exactly that, they treat 'inline'
as just a suggestion.
As far as recursive functions are concerned, you can not include its
code as part of its code, so no actual inlining can take place,
regardless of the presence of the 'inline' keyword.
Actually, some compilers, including VC++, I think, will inline recursive
functions by unrolling the recursion to a certain depth and then using a
call if the recursion goes deeper at runtime. There are also many recusive
functions that are "tail recursive", and a compiler can inline such
functions by converting the recursion into a simple iteration. I'm fairly
certian that VC++ does this optimization as well.

-cd

Loading...