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

A question about inline

P: n/a
Hello experts!

I'm reading a book about C++ and there is something about inline that the
book says that is unclear for me.

The book says the following "Because inline functions are expanded at
compile time, definitions of these
functions, unlike other definitions, cannot be separately compiled and must
be placed in header files.
This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same function).
Therefore, you have to check that the inline functions will actually be
inlined, and if they
cannot be inlined, you must remove the inline specification."

Here is the question about the text above that has been extracted from the
book. I understand the first sentence but not the second sentence. It says
"This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same function)".
I'don't understand how it is possible to end up with two definitions?

Sentence three says "Therefore, you have to check that the inline functions
will actually be inlined, and if they
cannot be inlined, you must remove the inline specification.". How can I
check that a function is really inlined?

Many thanks!!

//Tony
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Well, you could look at the assembly code, but it sounds to me like
your book is full of crap.

As far as I know, if the compiler gives you a warning it's not
standards compliant. I'm pretty sure something is allowed to be
multiply defined if it the definitions are token-by-token identical.

Jul 23 '05 #2

P: n/a
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:O4*******************@newsb.telia.net...
I'm reading a book about C++ and there is something about inline that the
book says that is unclear for me. And for a good reason...
The book says the following "Because inline functions are expanded at
compile time, definitions of these
functions, unlike other definitions, cannot be separately compiled and
must be placed in header files. So far so good: the above is correct.
This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same function). Nearly correct: because the function(/its body) is defined in a header,
the compiler may generate compiled code for it multiple times.
E.g. once in the object file (.o) of each source file (.cpp)
that includes the header where the function is defined.
The same may occur with template functions (also typically
defined in a header), as well as some implicitly-generated
functions (e.g. constructors, destructors, copy-assignment members).

However, a modern C++ linker will typically discard the
redundant object-code copies of the same function, and
in any case no error shall be reported.
Therefore, you have to check that the inline functions will actually be
inlined, and if they
cannot be inlined, you must remove the inline specification."
You could check if inlining has occured, in multiple platform-specific
ways (by somehow inspecting the generated code or link map, or by
using a specific output possibly provided by your compiler.

However, wheter inlining has occured or not is compiler-dependent,
and even compile-option dependent (e.g. inlining may be disabled
in debug builds). In any case, 'inline' is only a *hint* to the
compiler. Like the now pointless 'register' keyword, 'inline' is
destined to become less and less relevant as optimizing compilers
and linkers are getting smarter.
Here is the question about the text above that has been extracted from the
book. I understand the first sentence but not the second sentence. It says
"This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same
function)". I'don't understand how it is possible to end up with two
definitions? Only one definitions, but eventually redundant copies of the compiled code.
Sentence three says "Therefore, you have to check that the inline
functions will actually be inlined, and if they
cannot be inlined, you must remove the inline specification.". How can I
check that a function is really inlined?

You don't bother doing so.
One would typically only inline "trivial" functions such as simple
accessor member functions, and not bother about the final result
(until eventually having to profile and optimize the code).
I hope this helps clarify things,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #3

P: n/a
"Tony Johansson" <jo*****************@telia.com> wrote in message
news:O4*******************@newsb.telia.net
Hello experts!

I'm reading a book about C++ and there is something about inline that
the book says that is unclear for me.

The book says the following "Because inline functions are expanded at
compile time, definitions of these
functions, unlike other definitions, cannot be separately compiled
and must be placed in header files.
This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same
function). Therefore, you have to check that the inline functions
will actually be inlined, and if they
cannot be inlined, you must remove the inline specification."

Here is the question about the text above that has been extracted
from the book. I understand the first sentence but not the second
sentence. It says "This creates a problem if the compiler does not
actually inline a funktion(you may end up having multiple definitions
of the same function)". I'don't understand how it is possible to end
up with two definitions?
Because if the definition is in the header file and the header file is
#included in two different files, then you end up with two copies of the
function definition.
Sentence three says "Therefore, you have to check that the inline
functions will actually be inlined, and if they
cannot be inlined, you must remove the inline specification.". How
can I check that a function is really inlined?


Your book is talking rubbish. It is true that the compiler may not choose to
actually inline a function. However, if it does not, then it is up to the
compiler to prevent this from causing any problems. You don't need to do
anything.

--
John Carson

Jul 23 '05 #4

P: n/a
* Tony Johansson:

I'm reading a book about C++ and there is something about inline that the
book says that is unclear for me.
Please report here which book that is before you burn it.

The book says the following "Because inline functions are expanded at
compile time, definitions of these
functions, unlike other definitions, cannot be separately compiled and must
be placed in header files.
This creates a problem if the compiler does not actually inline a
funktion(you may end up having multiple definitions of the same function).
Therefore, you have to check that the inline functions will actually be
inlined, and if they
cannot be inlined, you must remove the inline specification."


The above is rubbish.

'inline' tells the compiler that you're taking responsibility for ensuring
that all definitions are the same. Multiple definitions are then ignored,
and that's the main purpose of 'inline'. See section 3 of chapter 2.1 at
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_02.html>.

Also, see the FAQ items 9.1 through 9.9 at <url:
http://www.parashift.com/c++-faq-lite/inline-functions.html> for the
optimization-side of things. Unfortunately the FAQ doesn't discuss the main
purpose of 'inline'. Perhaps that's because it's a Frequently Asked
Questions, not a What You Need To Know, and people ask about things like that.

--
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?
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.