473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"inline"?

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
14 2730
> 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
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
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
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
[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
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
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Rambo | last post by:
Hi, I made the following test program: //////////////////////// classes_1.cpp #include <iostream> #include "classes_1.h" using namespace std; A::A():i(0){cout <<"const A: i =" << i <<endl;}...
10
by: Christian Staudenmayer | last post by:
Hi, is there any revision of the C standard that allows the "inline" keyword (or a similar feature)? I know it is possible in gcc, but then it might be a gcc feature only. Greetings, Chris ...
14
by: Frederick Gotham | last post by:
The original purpose of "inline" was that code could be "expanded in place". Of course, it has other uses... For one thing, the following two translation units will compile together succesfully...
2
by: Steve Richter | last post by:
I would like to use display:inline and other CSS attributes to build an entry form. Where the heading to the left of the text box is always a set width. It is not working so I am experimenting...
12
by: Dave H. | last post by:
Please redirect me if this message is posted to the wrong group. Given the intention of delivering content to an HTTP user agent (such as Internet Explorer) which is to be immediately opened by...
2
by: mandarchalke29 | last post by:
Can you please tell me that document.getElementById("").style.display="Inline" what does this statement will do.
3
by: Baron Samedi | last post by:
I am looking for a reliable cross-browser pull-quote solution in CSS. See for instance, http://www.sitepoint.com/examples/pullquotes/ The problem seems at first to be sure that it functions...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.