473,549 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does inlining work?

While I have a very good feel for how inlining works, I fail to see how
in the world inlining can work if the inlined function is not described
"in place" in a header file, but rather defined in a separate source
file (using the inline keyword), which gets linked in? Does inling
work at all in such cases? If it does, can someone kindly explain how
the compilers handle that? If it does not, is that documented
somewhere?

Thanks,
Gus

Sep 14 '05 #1
15 2027
Generic Usenet Account wrote:
While I have a very good feel for how inlining works, I fail to see
how in the world inlining can work if the inlined function is not
described "in place" in a header file, but rather defined in a
separate source file (using the inline keyword), which gets linked
in? Does inling work at all in such cases? If it does, can someone
kindly explain how the compilers handle that? If it does not, is
that documented somewhere?


The definition of an inline function must be present and must be identical
in every translation unit (i.e., source file) in which it is called. If you
call an inline function in multiple source files the obvious way to do this
is to place the definition in a header file and #include the header in each
of the source files, but if you want, which you probably don't, you can
explicitly define the function separately and identically in each source
file.

I sometimes place an inline function in a source file rather than a header
file if I don't use it anywhere else (e.g., a private class member
function).

DW
Sep 14 '05 #2
Generic Usenet Account wrote:
While I have a very good feel for how inlining works, I fail to see how
in the world inlining can work if the inlined function is not described
"in place" in a header file, but rather defined in a separate source
file (using the inline keyword), which gets linked in? Does inling
work at all in such cases? If it does, can someone kindly explain how
the compilers handle that? If it does not, is that documented
somewhere?

Thanks,
Gus


No, the inline keyword alone is not enough to let a function call be
inlined.

In order to inline a function the compiler must also have the
function's implementation on hand. For this reason inline functions are
declared in header files. If the compiler has not seen the inlined
function's body when it compiles a call to that function, the function
call will not be inlined.

Failing to inline a function call is not considered an error; generally
there wil be no indication that a function call was not inlined. But
most C++ compilers have a setting to report an error whenever the
compiler was unable to inline a function.

Greg

Sep 14 '05 #3
* Generic Usenet Account:

While I have a very good feel for how inlining works,
Uh, you don't. ;-)

I fail to see how
in the world inlining can work if the inlined function is not described
"in place" in a header file, but rather defined in a separate source
file (using the inline keyword), which gets linked in?
That is not allowed.

Does inling work at all in such cases?
If you remove the 'inline' keyword so as to get a conforming program text,
then yes, it can work. That's called "whole program optimization", or "global
optimization". For example, Visual C++ from Microsoft does this.

If it does, can someone kindly explain how the compilers handle that?
Generally it's the linker that takes care of this job, because only the linker
has the necessary information available. If or when we get modules into the
language, possibly in C++0x, the compiler may be able to contribute more. Or
not, depending on how the standardization process turns out.

If it does not, is that documented somewhere?


If you mean, whether your invalid use of the 'inline' keyword is documented as
invalid anywhere, then that's the C++ standard. If you mean, is whole program
optimization documented somewhere, that'll be your compiler's documentation.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 14 '05 #4

Alf P. Steinbach wrote:
I fail to see how
in the world inlining can work if the inlined function is not described
"in place" in a header file, but rather defined in a separate source
file (using the inline keyword), which gets linked in?


That is not allowed.


I disagree. This is most certainly allowed by most compilers ----
whether it works or not as desired is a different issue altogether!

Thanks,
Nimmi

Sep 15 '05 #5
* Nimmi Srivastav:

Alf P. Steinbach wrote:
I fail to see how
in the world inlining can work if the inlined function is not described
"in place" in a header file, but rather defined in a separate source
file (using the inline keyword), which gets linked in?


That is not allowed.


I disagree.


Seemingly you don't understand that this is a matter of _fact_, as opposed to
a matter of opinion. So, cough up chapter and verse from the standard.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 15 '05 #6
> So, cough up chapter and verse from the standard.

I thought you knew already where in the standard that is as it looks
like you just read the chapter and verse carefully no doubt.

To Nimmi: please can you give a quote too as I'm handicapped by lack of
the standard document at the moment as I didn't have the foresight to
copy the file to my laptop before traveling. :)

I don't remember it being undefined to have inline specifier in
function or method declaration, but then again, the standard is over
700 pages.. <- sorry, I am just a human being and not have a perfect
memory STILL I work writing software sometimes in c++, does that make
_me_ incompetent? Should I resign now? Anyway, since the topic popped
up curious to get the topic laid to rest with facts. Thanks guys,
whoever helps me first. ;----)

Sep 15 '05 #7
"persenaama " <ju***@liimatta .org> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
So, cough up chapter and verse from the standard.
I thought you knew already where in the standard that is as it looks
like you just read the chapter and verse carefully no doubt.

To Nimmi: please can you give a quote too as I'm handicapped by lack of
the standard document at the moment as I didn't have the foresight to
copy the file to my laptop before traveling. :)


* * * * *
7.1.2
2 A function declaration (8.3.5, 9.3, 11.4) with an inline specifier
declares an inline function. The inline specifier indicates to the
implementation that inline substitution of the function body at the point of
call is to be preferred to the usual function call mechanism. An
implementation is not required to perform this inline substitution at the
point of call; however, even if this inline substitution is omitted, the
other rules for inline functions defined by 7.1.2 shall still be respected.

3 A function defined within a class definition is an inline function. The
inline specifier shall not appear on a block scope function declaration.(79 )

4 An inline function shall be defined in every translation unit in which it
is used and shall have exactly the same definition in every case (3.2).
[Note: a call to the inline function may be encountered before its
definition appears in the translation unit. ] If a function with external
linkage is declared inline in one translation unit, it shall be declared
inline in all translation units in which it appears; no diagnostic is
required. An inline function with external linkage shall have the same
address in all translation units. A static local variable in an extern
inline function always refers to the same object. A string literal in an
extern inline function is the same object in different translation units.
* * * * *

The reference to external linkage doesn't make sense to me yet. TC++PL 3rd
Ed. says (p. 199) that the "combinatio n of external linkage and inlining is
banned to make life simpler for compiler writers:"
// file1.c:
extern inline int g(int i);
int h(int i) { return g(i); } // error: g() undefined in this
translation unit
// file2.c
extern inline int g(int i) { return i+1; }
I don't remember it being undefined to have inline specifier in
function or method declaration, but then again, the standard is over
700 pages.. <- sorry, I am just a human being and not have a perfect
memory STILL I work writing software sometimes in c++, does that make
_me_ incompetent? Should I resign now? Anyway, since the topic popped
up curious to get the topic laid to rest with facts. Thanks guys,
whoever helps me first. ;----)


DW

Sep 15 '05 #8
Thanks for the quote, it takes a couple of readings to decrypt what
they intended to say, actually I still didn't quite get what their
*intention* was but according to these rules what mr. Stenbach says is
not strictly incorrect.. as long as the *definition* is inline in every
translation init all's well as far as the standard is concerned.

Quote from the quote (thanks!): "If a function with external
linkage is declared inline in one translation unit, it shall be
declared
inline in all translation units in which it appears; no diagnostic is
required."

The keyword here is *declared*, declared.. that will hold up true
indeed if the same header is included in other translation units (note:
I am not claiming that files or headers exist they are just figment of
implementations imagination ;-) So far so good, the original poster's
hypothesis (?) doesn't invalidate this rule..

Earlier in the same paragraph this is stated:

"4 An inline function shall be defined in every translation unit in
which it
is used and shall have exactly the same definition in every case
(3.2)."

Indeed, if the *definition* is in other translation unit, in this case
I read between the lines that in a different sourcefile (okay, again,
implementation specific detail but that's how I interpreted the
original post, call me incompetent ;) So far again I don't see any
gross neglegance or violation of the standard in the original post,
just construct which requires every other translation unit to have the
*same* definition, this is rather difficult to enforce unless the same
sourcecode is part of the other translation units being compiled. The
common method to achieve this is to put the inline code in a header
file, often the declaration and definition are the same thing.. or the
definition comes later in the same file, or, separate implementation
file is used, .inl is what I encountered myself now and then (not my
sourcecode heaven's sake, I combine the definition with declaration
more often than not :)

All in all, I don't seem to see where the actual error is, I only see
where it could possibly be but w/o any actual sourcecode to discuss..

The example you posted is a good case, btw. Side effects, linkage,
undefined behaviour and such aside, one way I can see this could be
implemented without much fuss is that the compiler merely ignores the
inline keyword. It's free to do so at any time of it's liking for code
generation purposes anyhow. The only difficulty is that the standard
itself disagrees (?) with the TC++PL.. I'd say I'd trust the standard
more in this case as that's what all implementations are supposed to be
implementing (?) I'm very likely to be wrong since I am way out of my
league here.. but I got asshole and I got opinions and this is a
discussion group so there! ;-)

Sep 15 '05 #9

persenaama schreef:
Thanks for the quote, it takes a couple of readings to decrypt what
they intended to say, actually I still didn't quite get what their
*intention* was but according to these rules what mr. Stenbach says is
not strictly incorrect.. as long as the *definition* is inline in every
translation init all's well as far as the standard is concerned.
You are aware that every definition is also a declaration? There's
no word for declarations-that-aren't-definitions, although "forward
declaration" comes close.

The only difficulty is that the standard
itself disagrees (?) with the TC++PL.. I'd say I'd trust the standard
more in this case as that's what all implementations are supposed to be
implementing (?)


Assuming you have a 3rd ed, there is no disagreement. And yes,
implementations are supposed to implement the standard. Mr. Stroustrup
has very good ideas, but he isn't the ultimate authority.

HTH,
Michiel Salters

Sep 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1621
by: glen_stark | last post by:
Hi. I'm just curious if there any warnings or caveats or whatever to be aware of when inlining function object calls? If the answer is no, they inline just like everything else, that's good news for me. glen
10
2322
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O bounded or user-time bounded (i.e. windows based applications) but they are concurrently involved in the execution of the same parallelized task (i.e....
55
2746
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
5
1460
by: Raphael | last post by:
Hello, I have 2 files A.c : Defining a bunch of usefull functions B.c : Defining a main that uses these ones. I need to improve perfs : I know that, if the caller and the functions called were in the same file, the compiler would automatically inline them if possible.
12
1407
by: Tomás | last post by:
The common persuasion is: Big function -- leave it outline. Small function -- make it inline. In code I'm writing at the moment, I have a fairly long function, so it wouldn't cross my mind to make it inline. However, the function is invoked only once, and its invokation is at the very beginning of the
21
1777
by: LuB | last post by:
How judicious ought one be when inlining small methods. I once read that in general, most compiles will only inline 'one' level. IE: if all the following methods were declared/defined as inline in their respective classes, can I expect the compiler to try and inline them all? obj.GetSize()
15
3035
by: Lloyd Dupont | last post by:
I have some code which looks like that: public CornerStyle RectCornerMode { get { return this.GetValue<CornerStyle>(); } set { this.SetValue<CornerStyle>(value); } }
21
2017
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple constructors... any thoughts Mike
58
4863
by: sh.vipin | last post by:
is there any way to find out number of bytes freed on a particular free() call in C
0
7960
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7812
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6048
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5089
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.