A question about inline

T

Tony Johansson

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
 
E

Evan

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

John Carson

Tony Johansson said:
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.
 
I

Ivan Vecerina

Tony Johansson said:
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
 
A

Alf P. Steinbach

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

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,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top