| re: about extern inline and static inline
Sean wrote:[color=blue]
> Jonathan Mcdougall wrote:[color=green]
> > Sean wrote:
> >
> > inline
> > tells the compiler to try to inline that function (it may refuse for
> > various reasons)
> >
> > extern
> > redundant for a function but for an object, transforms a definition
> > into a declaration
> >
> > static
> > for a namespace-scope name (not in a class), makes it local to the
> > translation
> > unit (roughly the file in which it is defined). However, this is
> > deprecated in C++,
> > unnamed namespaces should be used instead.[/color][/color]
I have to change that last sentence. Only using static on objects in a
namespace scope is deprecated. Using static on functions is legal.
[color=blue][color=green]
> > static inline and static extern are both combinations of the two terms.[/color][/color]
[color=blue]
> but if extern inline means "extern + inline" (simple combination), there
> exist two definition of incr_i() in my example--since myheader.h is
> included in main.cpp and call_inc.cpp.
> There must be some special meaning besides simple combination, I guess.[/color]
No. As I said, extern for a function is redundant (a function always
has external linkage, unless it is static) so let's forget about it. If
a function is inline, its *definition* (body) is actually *required* in
each translation unit it used. Think: if the definition is not
available, how can it be inlined? Multiple definitions (in different
translation units) of an inline function is not only legal, but
required.
If the function is both static and inline, it may confuse the compiler,
which will probably decide not to inline the function. Because the
function is static, each translation unit will get its copy. If the
compiler honors the inline hint, each translation unit will have its
copy of the function inlined.
Jonathan |