By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

"inline"?

P: n/a
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"?
Jul 22 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
> 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...
Jul 22 '05 #2

P: n/a
Marko Becirevic writes:
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...


Change "will" to "may". inline is advice to the compiler, not a mandate.
Jul 22 '05 #3

P: n/a
Marko Becirevic wrote:
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...


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

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #4

P: n/a
Artie Gold wrote:
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.

Jul 22 '05 #5

P: n/a
[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.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #6

P: n/a
Cy Edmunds wrote:
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.


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?
which comes into play during linking if not compiling.


Jul 22 '05 #7

P: n/a
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message news:<3F**************@jpl.nasa.gov>...
Cy Edmunds wrote:
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.


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?
which comes into play during linking if not compiling.

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)

---
Jared Dykstra
http://www.bork.org/~jared
Jul 22 '05 #8

P: n/a
Jared Dykstra wrote:
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:<3F**************@jpl.nasa.gov>...
Cy Edmunds wrote:
>>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.
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?
> which comes into play during linking if not compiling.

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.

Jul 22 '05 #9

P: n/a
Why can't I use external inline functions? I read that in Stroustrup's
book (The C++ Programming Language: 3rd Edition)...
Jul 22 '05 #10

P: n/a
E. Robert Tisdale wrote:
Cy Edmunds wrote:
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.

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.
Thus there is a real, non-optional difference

I'm not sure what you mean here.
Difference between inline and static functions?
which comes into play during linking if not compiling.



Jul 22 '05 #11

P: n/a
Rolf Magnus wrote:
Jared Dykstra wrote:

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!
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.

Jul 22 '05 #12

P: n/a
Chris Mantoulidis wrote:
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.

Jul 22 '05 #13

P: n/a
Rolf Magnus <ra******@t-online.de> wrote in message news:<br*************@news.t-online.com>...

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.
Jul 22 '05 #14

P: n/a
In article <ba**************************@posting.google.com >,
dy******@hotmail.com says...

[ ... ]
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...

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.