Connecting Tech Pros Worldwide Forums | Help | Site Map

A question about inline

Tony Johansson
Guest
 
Posts: n/a
#1: Jul 23 '05
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





Evan
Guest
 
Posts: n/a
#2: Jul 23 '05

re: A question about inline


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.

Ivan Vecerina
Guest
 
Posts: n/a
#3: Jul 23 '05

re: A question about inline


"Tony Johansson" <johansson.andersson@telia.com> wrote in message
news:O4O3e.21142$d5.156815@newsb.telia.net...[color=blue]
> I'm reading a book about C++ and there is something about inline that the
> book says that is unclear for me.[/color]
And for a good reason...
[color=blue]
> 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.[/color]
So far so good: the above is correct.
[color=blue]
> This creates a problem if the compiler does not actually inline a
> funktion(you may end up having multiple definitions of the same function).[/color]
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.
[color=blue]
> 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."[/color]

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.
[color=blue]
> 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?[/color]
Only one definitions, but eventually redundant copies of the compiled code.
[color=blue]
> 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?[/color]
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


John Carson
Guest
 
Posts: n/a
#4: Jul 23 '05

re: A question about inline


"Tony Johansson" <johansson.andersson@telia.com> wrote in message
news:O4O3e.21142$d5.156815@newsb.telia.net[color=blue]
> 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?[/color]

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.
[color=blue]
> 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?[/color]

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

Alf P. Steinbach
Guest
 
Posts: n/a
#5: Jul 23 '05

re: A question about inline


* Tony Johansson:[color=blue]
>
> I'm reading a book about C++ and there is something about inline that the
> book says that is unclear for me.[/color]

Please report here which book that is before you burn it.

[color=blue]
> 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."[/color]

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?
Closed Thread