Re: about inline functions



CBFalconer wrote:
Christian Kandeler wrote:
s0suk3@xxxxxxxxx wrote:
....
But inline avoids a problem when you want to have a header file
that contains an inline function, and that header is included
in more than one file.
But you can achieve roughly the same effect by declaring the function static.

'static' for a function has nothing to do with inline. It simply
means that the functions is not accessible from outside the source
file in which it is declared, except when specifically passed out
as a pointer to a function.

Yes and no. 'inline' is just a hint to the compiler. The point is, a static function provides very nearly the same hint.

A static function, often doesn't have to be an actual function - it could be in-lined at each call, and a good optimizing compiler will do just that, taking advantage of the as-if rule. Some features of the code can prevent inlining it, such as taking a pointer to the function, especially if that pointer is passed to code in some other translation unit. However, if you have a function for which 'inline' is a good idea, it's often the case that simply declaring it 'static' without the 'inline' keyword is sufficient to allow the compiler to perform this optimization.

It was for this reason that many people opposed the creation of the 'inline' keyword. Using 'static' doesn't guarantee that the function will be inlined, even if there's no good reason not to - but the same can be said of the 'inline' keyword.
.



Relevant Pages

  • Re: What is the gain of "inline"
    ... Even then it is not quite redundant in that the compiler can diagnose ... constraints when the function is marked as inline. ... The "auto" keyword is completely useless (except perhaps as ... The "register" keyword, on the other hand, is at least potentially ...
    (comp.lang.c)
  • Re: inline vs. function pointers
    ... it will crash because you *SPECIFICALLY TOLD* the compiler ... to inline all those function calls. ... If inlining a particular function would make it slower, then for that particular function, the "inline" keyword is a suggestion that inlining NOT be performed, counterintuitive though that may seem. ... If the programmer specifically requests that a function ...
    (comp.lang.c)
  • Re: VC++2003: Methods implemented outside class body are never inlined for templates that are instan
    ... the 'inline' keyword is strictly an advisory to the ... >> the compiler precluded from inlining code not so marked. ... introduced with the inline keyword the compiler is free to ignore any ... > T fooconst ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Disadvantage of using inline
    ... Declaring a function as 'inline' never meant that is has no "independent ... The decision to inline a call is made by the compiler on a per-call ... >> machines, there are so few registers that tying one up is stupid), ...
    (comp.lang.c)
  • Re: Load-time evaluation of compound form operators.
    ... finer-grained way of declaring things than NOTINLINE and INLINE. ... NOTINLINE says pretty much "assume nothing, you need to look it up every ... this declaration would enable the compiler to assume as much ...
    (comp.lang.lisp)