inline functions

M

Mani

Experts, I would like to know how inline, 'static inline' and 'extern
inline' functions differs. Which one should be used when.
 
A

Armel

Mani said:
Experts, I would like to know how inline, 'static inline' and 'extern
inline' functions differs. Which one should be used when.

static and extern are totally independent from "inline".

static tells that the function is local to the module (for global
functions), or class functions in opposition to object functions (for
functions declared inside a struct/union/class definition)

extern is typically not used with functions prototypes but i believe some
compilers use that to declare the function being part of the external
interface of the built program (e.g. public function of .DLL or .so)
 
Ö

Öö Tiib

Experts, I would like to know how  inline, 'static inline' and 'extern
inline' functions differs. Which one should be used when.

There are no such things in C++ as 'static inline' and 'extern
inline'. These are C terms described in C99. So if you want to know
the details read that standard or ask in C group.

In C++ 'inline' keyword is an hint to compiler that the function may
be inlined. Compiler has still quite free hands to inline or not to
inline what it pleases as long it does not cause externally visible
side effects.

Biggest effects of 'inline' keyword in C++ are that the definition of
function must be in code before the code calling it and that inline
function may be defined in the header file without getting linker
errors about that function being defined in several translation units
that did include the header file.
 
J

Juha Nieminen

Öö Tiib said:
In C++ 'inline' keyword is an hint to compiler that the function may
be inlined. Compiler has still quite free hands to inline or not to
inline what it pleases as long it does not cause externally visible
side effects.

Actually, does any modern C++ compiler take the 'inline' keyword
*at all* into consideration when deciding whether to inline a function
or not? I have the impression that, from the optimization point of view,
the 'inline' keyword is a no-op (in the exact same way as eg. the
'register' keyword is). Hence the 'inline' keyword has basically lost
its meaning as an optimization hint keyword and become purely an
instruction for the linker.
Biggest effects of 'inline' keyword in C++ are that the definition of
function must be in code before the code calling it and that inline
function may be defined in the header file without getting linker
errors about that function being defined in several translation units
that did include the header file.

Also note that some functions are implicitly 'inline' even without
having to explicitly specify that keyword. More specifically, member
functions which are defined in the class definition, as well as templated
functions (ok, there might be some technical difference between a templated
function and an inline function, but the end result is basically the same).

Eg:

class SomeClass
{
public:
// This function is 'inline' without having to specify the
// keyword explicitly:
void foo() { std::cout << "hello\n"; }

void bar();
};

// This function is *not* 'inline' if you don't specify the keyword
// explicitly here (hence you can get a linker error if this is
// included in more than one compilation unit):
void SomeClass::bar() { "there\n"; }
 
V

Virchanza

Experts, I would like to know how  inline, 'static inline' and 'extern
inline' functions differs. Which one should be used when.


Forgetting about template functions for a moment (which work a little
differently):

Every function, whether it be inline or not, is either "extern" or
"static".

"extern" means that the function is available outside of the
translation unit in which it is defined.

"static" means that the function is only available inside the
translation unit in which it is defined.

By default, all functions are "extern" unless you explicitly write the
word "static" in front of them.

The following function:

inline int Func() { return 5; }

is the same as:

extern inline int Func() { return 5; }

You can however define an inline function as static:

static inline int Func() { return 5; }

The "static" keyword won't have any noticeable effect upon an inline
function unless the function contains a static object, e.g.:

static inline int Func()
{
static int i = 0;

return i++;
}

Because Func is static, there will be a separate "i" object for every
translation unit that defines this function. Normally, if Func were
extern, there would only be one "i" object for the entire program.
 
S

SG

Experts, I would like to know how  inline, 'static inline' and 'extern
inline' functions differs. Which one should be used when.

They differ in linkage. The "inline" keyword has little to do with
this. These two concepts are orthogonal.

Linkage determines whether you can refer to an entity directly by name
from another translation unit (external linkage) or not (internal
linkage). Some prefer to use other terms like "global scope" and "file
scope" to differentiate between external and internal linkage. Note
also that the keyword "static" has three different uses. Its meaning
changes depending on the context. One of them is to give entities
internal linkage. As such, it's kind of the opposite of "extern" in
that case. Many things have external linkage by default in which case
the use of "extern" would be redundant.

But in C (not C++) "inline" usually goes with "static" because of the
way inline works in C. In C++ you can simply put a definition of an
inline function into a header file and include it into multiple
translation units without having the linker complain to you about
multiple definitions. The ODR (one definition rule) makes an exception
for inline functions and template functions. What you have to keep in
mind, though, is that if you use an inline function in a translation
unit, this translation unit also has to contain a definition for it. I
would go as far as saying that this is actually the point of "inline".
Actual inlining for a compiler is made easy if it has easy access to
the function's definition. The inline keyword is a way of making sure
the definition is available in the same translation unit.
 
J

Juha Nieminen

Virchanza said:
static inline int Func() { return 5; }

The "static" keyword won't have any noticeable effect upon an inline
function unless the function contains a static object, e.g.:

Well, strictly speaking the value of the function pointer will
depend on the compilation unit as well. (Not that making this
distinction is anything common.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top