"inline"?

C

Chris Mantoulidis

I am not clear with the use of the keyword inline... I believe you add
it do a function when you implement the function inside the header
file where the class is stored...

But is that all? What am I missing?

If that's all, then why did Bjarne even bother adding it to the
language?

If that's not all, what else can I do with "inline"?
 
A

Artie Gold

Marko said:
Inline means that function body will be copied everywhere where function is
called, so the time for calling function will not be spent. It can be
usefull in loops where you non-stop call same function, or something like
that...

Well, close. ;-)

It's a *suggestion* to the compiler to inline the function (just how
aggressively a given compiler will honor such request is often
controllable by compiler specific options).

One case in which inlining is often valuable is, as you've said, when a
function is called in a tight loop; another is when a small function is
called frequently throughout the code -- and its code is sufficiently
small that the standard calling sequence would be more expensive than
the code itself.

With the exception of tight loops and trivial functions, inlining is the
sort of optimization that should usually be used only when a performance
bottleneck has been discovered through profiling.

Also be aware that inlining a recursive function (whether the recursion
is direct or indirect) is, obviously, a `no-go'.

HTH,
--ag
 
O

osmium

Marko said:
Inline means that function body will be copied everywhere where function is
called, so the time for calling function will not be spent. It can be
usefull in loops where you non-stop call same function, or something like
that...

Change "will" to "may". inline is advice to the compiler, not a mandate.
 
E

E. Robert Tisdale

Artie said:
Also be aware that inlining a recursive function
(whether the recursion is direct or indirect) is, obviously, a `no-go'.

Nonsense! A good optimizing C++ compiler
will "flatten" the recursion and inline it.

According to Bjarne Stroustrup,
"The C++ Programming Language: Third Edition",
Chapter 7: Functions, Section 1: Function Declarations,
Subsection 1: Function Definitions, page 144.

A good optimizing C++ compiler may inline a function
even if the programmer does not specify inline
as long as the C++ compiler can "see" the function definition.
Certainly, any function defined in the same translation unit
can be seen by the compiler but new optimizing compilers
may be able to find function definitions
in other translation units and inline them.

So what is the inline qualifier good for?
Why not just define functions static
and let the compiler decide whether to "inline" them or not?

Chapter 9: Source Files and Programs, Section 2: Linkage,
page 199 helps to answer these questions.
 
M

Marko Becirevic

I am not clear with the use of the keyword inline... I believe you add
it do a function when you implement the function inside the header
file where the class is stored...
If that's not all, what else can I do with "inline"?

Inline means that function body will be copied everywhere where function is
called, so the time for calling function will not be spent. It can be
usefull in loops where you non-stop call same function, or something like
that...
 
C

Cy Edmunds

[snip]
So what is the inline qualifier good for?
Why not just define functions static
and let the compiler decide whether to "inline" them or not?


You can put an inline function in a header and only get one instance no
matter how many times the header is included. Thus there is a real,
non-optional difference which comes into play during linking if not
compiling.
 
E

E. Robert Tisdale

Cy said:
You can put an inline function in a header and only get one instance
no matter how many times the header is included.

Wouldn't you get the same
if the function was simply declared static instead of inline?
Thus there is a real, non-optional difference

I'm not sure what you mean here.
Difference between inline and static functions?
 
J

Jared Dykstra

E. Robert Tisdale said:
Wouldn't you get the same
if the function was simply declared static instead of inline?


I'm not sure what you mean here.
Difference between inline and static functions?


I think this has already been covered, but I'll reiterate. It is most
useful to think of the difference from the linker's point of view.

A static function is one in which only one instance is created. No
matter how you access it functions API, it is always the same code at
the same program counter location which will be executed.

An inline function is not really a function at all; it only looks that
way in your source code. The function call is removed, and a copy of
the body of the inline function inserted directly at that point. It's
a little bit like unrolling a loop. If your inlined function is
called all over the place, a new copy of that function is created for
each call.

You don't get anything for free. The speed savings come at a cost of
larger binaries unless the function is only referenced once.

Take away three conclusions from this thread; the last is most
important:
* Don't use inlines unless you know what they do and all of the
implications
* Choosing to use the inline code or not doing carries no guarantees
but may improve performance.
* Premature optimization is the root of all evil (Knuth)
 
R

Rolf Magnus

Jared said:
I think this has already been covered, but I'll reiterate. It is most
useful to think of the difference from the linker's point of view.

A static function is one in which only one instance is created. No
matter how you access it functions API, it is always the same code at
the same program counter location which will be executed.

No. A static function has one single "program counter location" in the
translation unit where it's defined. If you want to use that function
in more than one translation unit, each of them will need its own
definition, since the linker won't see any static functions at all.
And if the compiler has the code for a function, it may -- even without
the inline specifier -- choose to inline this function.
An inline function is not really a function at all; it only looks that
way in your source code. The function call is removed, and a copy of
the body of the inline function inserted directly at that point.

Depends on what you mean by "An inline function". If you mean a function
defined as 'inline', then this may not be the case. If you instead mean
a function that is actually inlined, you're right. The inline keyword
is only a hint to the compiler that you'd like this funciton to be
inlined, but the compiler is free to inline functions you didn't
declare as inline or to not inline functions you did. Of course it will
need the code of that function to be able to inline it, so this code
must be know in the translation unit.
It's a little bit like unrolling a loop. If your inlined function is
called all over the place, a new copy of that function is created for
each call.

You don't get anything for free. The speed savings come at a cost of
larger binaries unless the function is only referenced once.

There may even be no speed savings at all. Inlining can easily lead to
slower execution. Same for loop unrolling. Because of the increased
code size, you will get more cache misses in the CPU's code cache, and
ineffective cache use can have a dramatic impact on execution speed.
 
C

Chris Mantoulidis

Why can't I use external inline functions? I read that in Stroustrup's
book (The C++ Programming Language: 3rd Edition)...
 
J

Jeffrey Schwab

E. Robert Tisdale said:
Wouldn't you get the same
if the function was simply declared static instead of inline?

No. The function's body is (read "may be") duplicated once per call of
the function, not just once per TU.

But this does bring me to a question I have, which I will post separately.
 
E

E. Robert Tisdale

Rolf said:
Jared Dykstra wrote:

No!
The optimizing C++ compiler is free to inline static functions
in *exactly* the same way that it inlines functions
explicitly defined to be inlined.
A static function has one single "program counter location"
in the translation unit where it's defined.
No.
If you want to use that function in more than one translation unit,
each of them will need its own definition,

But the definition of a static function could be different
in different translation units.
The definition of an inline function *must* be the same
in every translation unit. Unfortunately,
this restriction is very difficult for compilers to check
so it can only be interpreted as advice to programmers.
since the linker won't see any static functions at all.
And if the compiler has the code for a function, it may
-- even without the inline specifier --
choose to inline this function.

Correct.
 
R

Rolf Magnus

Chris said:
Why can't I use external inline functions? I read that in Stroustrup's
book (The C++ Programming Language: 3rd Edition)...

You can declare them as inline, but they won't be actually inlined.
 
J

Jared Dykstra

Rolf Magnus said:
Depends on what you mean by "An inline function". If you mean a function
defined as 'inline', then this may not be the case. If you instead mean
a function that is actually inlined, you're right.

The fact the the inline attribute is only a hint has been mentioned
over, and over, and over. In fact I even mentioned it in my previous
post, had you read the complete article.

Obviously if a function specified to be inlined was not, it is no
different than a regular function, requiring no further debate.
 
J

Jerry Coffin

[ ... ]
The fact the the inline attribute is only a hint has been mentioned
over, and over, and over. In fact I even mentioned it in my previous
post, had you read the complete article.

Obviously if a function specified to be inlined was not, it is no
different than a regular function, requiring no further debate.

An inline function is slightly different from a non-inline function,
regardless. If you put 'inline' on a function that the compiler doesn't
generate inline, it still has a slight effect on the semantics. If the
compiler generates inline code for a non-inline function, that still
doesn't give quite the same semantics.

Admittedly, the change in semantics due to using 'inline' is so slight
that it often escapes notice, but try putting a non-inline function in a
header and then include that header in two (or more) files, and link the
results. Then try adding 'inline' and see if it doesn't change the
result a bit...
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top