On 28/09/2012 07:39, Kaz Kylheku wrote:
> On 2012-09-28, Johann Klammer <
klam...@NOSPAM.a1.net> wrote:
>> sinbad wrote:
>>> when would one use macros and when static inline,
>>> unless the static inline function is referenced
>>> by it's address, it is 'almost' like a macro with
>>> better readability, so why not use static inline
>>> as macros.
>> It is for micromanagement fanatics. The compiler is free to ignore the
>> inline, and it usually does if you compile for space optimisation and
>> call the function from two or more different places.
>>
>> If you want your compiler to decide, use static inline.
>> If you want it _always_ inlined, use macros.
>
> Even if you use static without inline, the compiler can inline anyway.
> (For that reason, compilers should really treat inline seriously and
> "do it", otherwise what is it for?)
>
In my experience, compilers /do/ treat "inline" seriously. They don't
treat it as a command (there are circumstances where it is impossible to
inline the function), but they usually treat it as a very heavy hint. So
unless you have forced the compiler to out-line the function (by using
recursion, taking its address, forgetting to enable optimisations,
etc.), then "inline" will normally do exactly that.
Compilers often have additional compiler-specific extensions (such as
gcc's "always_inline" and "flatten" attributes) to give even more
control over inlining.
As others have said, "static" functions may be inlined anyway - this is
particularly true for use-once functions when optimising, or very small
functions when optimising hard. In fact, the compiler can - if it wants
- inline non-static functions as well. It will have to generate
out-line versions too, but these can perhaps be garbage collected at
link time if they are not needed.