question on inlining

G

Geo

Alf said:
* Geo:

Generally wrong.

Really, why else then ?
I mentioned a few reasons on request from Ioannis in this thread.

Forgot one important there: the trend towards template oriented programming.

Yes, of course, but 'inline' predates 'template', nevertheless, the
template code is only included in multiple translation units for the
same reason, the compiler NEEDS to see it.
 
I

Ioannis Vranos

Alf said:
Generally wrong.

I mentioned a few reasons on request from Ioannis in this thread.

Forgot one important there: the trend towards template oriented programming.


Still, I am not satisfied by the answers. :)

If you may clarify to these, it would be great:



This is a tool subject, and the discussion turns to providing documentation by "hacking"
the language.


What kind of more technical documentation do you have in mind than this:


-- SomeFunc.h

// SomeFunc() does whatever
void SomeFunc();


-- SomeFunc.cpp

#include "SomeClass.h"

//...

void SomeFunc()
{
// ...
}



For something more, there are documentation tools out there (even for source code-level
documentation).



contrary, if you decide to change only one function definition, you will have to recompile
every compilation unit that uses it, if you have made it inline.
Yes, that's the "can cut both ways".

The advantage wrt. separate compilation is that with an 'inline' function in
a header file, there may be no need of an implementation file to be
separately compiled. That cuts down on both the programmer's work, on the
redundancy of declarations, and on the number of compiler invocations.




What kind of declarations does it cut down? As of compiler invocation, are you talking
about one additional file to compile while avoiding to recompile the rest when you decide
to change the definition, vs compile *all* the rest that make use of the function, once
you decide to modify the definition.

That is incorrect. 'inline' does not (necessarily) inline: inlining or not
is up to the compiler's discretion, regardless of 'inline'.



But it will inline whenever it can for the ones that you defined as inline and have placed
in scope before the function use in a translation unit, while it may not inline when you
make it a regular function with one definition in another translation unit.

In this way you will get more inlining, and not less.

What 'inline'
does is to deal with multiple definitions for non-template code, which is a
correctness issue as opposed to an optimization issue.



I am not sure I see any benefits of this. The way I view it, is that what you gain is only
extra pain to recompile everything on any change of the inlined function definition as
also possible extra, unintended code bloat, while there isn't any source code benefit of this.
 
C

Class that cannot be inherited ... C++

wht is ODR ?
what i guess with that is this
please reply after filter out my mistake

"
if in header file function body is given and it is not mentioned inline
so that function will be compiled again and again. and waste compliler
resource .
now what the sffect on code will be thr.
what i knows about header is that a function / class after compilation
will be in a obj file and a header file is for tell ur program the
signature of the functions at compile time . so if body of a function
is defined in the header file so how will it affect the compilation
process and that precomplied object file (.obj or .o)
 
G

Geo

Alf said:
* Geo:

See my (first) reply to Ioannis:

Sorry, but I can't see any other reason, the ONLY reason to include the
function source in a translation unit is for the compiler to see it,
and it only needs to see it more than once is if it's a candidate for
inlining, or it's a template.
 
C

Class that cannot be inherited ... C++

See Peter Koch Larsen's reply.

Do not rely on 'inline' as an optimization hint.


The keyword has nothing to do with optimization.


--
will u plese coorect me
properly inlineD code will help the compiler to waste less resources to
compile
or it give work to complier that inline is mentioned so go and cheack
it can be inlined or not.
now in both case
if in will make inline
so will save the execution time ata least
secondly
if we r not mentioning inline for a function so it will never make it
inline why?
 
A

Alf P. Steinbach

* Ioannis Vranos:
Still, I am not satisfied by the answers. :)

If you may clarify to these, it would be great:



This is a tool subject, and the discussion turns to providing
documentation by "hacking" the language.

Parse error.

What kind of more technical documentation do you have in mind than this:


-- SomeFunc.h

// SomeFunc() does whatever
void SomeFunc();


-- SomeFunc.cpp

#include "SomeClass.h"

//...

void SomeFunc()
{
// ...
}

Remove

* The comment (can't be checked by the compiler)
* The #include.
* The repeated declaration of the function signature.

Move

* The body of the function to the header file.

Add

* 'inline'.

Now the function (assuming it's a small one) is self-documenting.

For something more, there are documentation tools out there (even for source code-level
documentation).

The main documentation is always the compilable source code. Comments
are second class, because they can't be checked and easily get out of
synch in the cases where they're correct in the first place. Separate
documentation is even worse.


When you only have a header file, no implementation file, there is one
less file to compile separately.


[Below it seems something's wrong with the quoting levels. However]

Also, in the
contrary, if you decide to change only one function definition, you will have to recompile
every compilation unit that uses it, if you have made it inline.


What kind of declarations does it cut down?

It cuts down on _redundancy_ of declarations, the repeated function
signatures.

Redundancy is bad both because of more work, and because of the
possibility of unnoticed differences.

As of compiler invocation, are you talking
about one additional file to compile while avoiding to recompile the rest when you decide
to change the definition, vs compile *all* the rest that make use of the function, once
you decide to modify the definition.

Parse error. I gather it's an attempt at phrasing a false dilemma? ;-)

But it will inline whenever it can for the ones that you defined as
inline and have placed in scope before the function use in a
translation unit,

No, that is incorrect.

The point that's been stressed (I feel) a thousand times in this thread
is that not only can you not rely on that.

With modern compilers you can rely on the opposite: that the compiler
will do the job much better you can, and the more information you give
the compiler to do that job, the better job it does.

while it may not inline when you
make it a regular function with one definition in another
translation unit.

That's almost a given as of 2005, because few C++ compilers so far
do a decent "whole program optimization" which is needed for that.

Essentially that, for those hooked on needless optimization, one should
give the compiler the most information possible to do the very best job.

And that means preferably using 'inline' functions and in-class definitions.
:)

Lest I be misunderstood here: I'm not recommending that unconditionally,
only for the mindless micro-optimization-freaks.

In this way you will get more inlining, and not less.

The effect depends on everything. The complete program, the compiler,
system, options, everything. All you know is that _whatever_ you're asking
the compiler to optimize for, the more information you make available to it
(like the source code of functions), the better a job it will generally do.

I am not sure I see any benefits of this.

Naturally: if the code doesn't need to be correct it can be infinitely
optimized! Great idea. Correctness, goodbye!

The way I view it, is that what you gain is only
Incorrect.


extra pain to recompile everything on any change of the inlined
function definition

Yes, there I agree very much.

as also possible extra, unintended code bloat,

Nope. Or rather, anything's possible, but it's extremely unlikely. On
the contrary, if you the compiler to optimize for code space, it's much
more likely to do a perfect job if you give it maximum information.

while there isn't any source code benefit of this.

Incorrect.


Cheers,

- Alf
 
V

velthuijsen

Class said:
See Peter Koch Larsen's reply.

Do not rely on 'inline' as an optimization hint.


The keyword has nothing to do with optimization.
Read: http://www.gotw.ca/gotw/033.htm
or it give work to complier that inline is mentioned so go and cheack
it can be inlined or not.
now in both case
Yes it is no more then a hint for the compiler to check if it can be
inlined.
if in will make inline
so will save the execution time ata least
Read the caveat about inlining in the article.
secondly
if we r not mentioning inline for a function so it will never make it
inline why?

The keyword inline has another effect (besides hinting that you as
programmer would like to see the function inlined).
The code that you want to inline has to be visible in every
file/compilation unit that you use this code in.
If you do this with a normal function you get a linker error saying
that your function is already defined
try the following bit:

Code:
Header file: Foo.h
#ifndef FOO_H
#define FOO_H

void Bar(){static int x =0;++x;}

void Test();
#endif

code file: Foo.cpp
#include "Foo.h"
void Test()
{
Bar();
}

main: main.cpp
#include "Foo.h"
#include <iostream>

int main()
{
std::cout << getchar();
return 0;
}

This should get you the error.
This happens because basically the content of the header file gets
placed at the exact point where you do #include "Foo.h". This results
in the linker finding the function Bar twice.
If the words inline gets placed before the function Bar and it gets
inlined by the compiler the function Bar does not exist.
Since you cannot say 100% sure when a function gets inlined (the
compiler makes the final decision) the linker ignores multiple
definitions of the same function if the function has the inline keyword
infront of it.
In both cases the problem of multiple definitions is (re)solved.
 
I

Ioannis Vranos

Alf said:
Parse error.


Meaning, documentation is an entirely other subject, and it has not anything to do with
our subject. It is a matter of what documentation style one wants, and documentation tools.

Remove

* The comment (can't be checked by the compiler)
* The #include.
* The repeated declaration of the function signature.

Move

* The body of the function to the header file.

Add

* 'inline'.

Now the function (assuming it's a small one) is self-documenting.


Sorry but I can't understand this. For me the function definition remains the same
"self-documenting" (whatever this means) when you place it in one implementation file only.


When you only have a header file, no implementation file, there is one
less file to compile separately.


The first time. In the case of inlined functions however, the second time you will have to
recompile *all* the other files that make use of the function.


It cuts down on _redundancy_ of declarations, the repeated function
signatures.

Redundancy is bad both because of more work, and because of the
possibility of unnoticed differences.


You place the declaration only once, inside the same header that you would place the
redundant inlined definition, and include it as you would do in your (strange) inlining
approach. And you avoid the recompile-all when you modify the function definition.

Parse error. I gather it's an attempt at phrasing a false dilemma? ;-)


I think the parser is buggy, and not the data itself. :)



No, that is incorrect.

The point that's been stressed (I feel) a thousand times in this thread
is that not only can you not rely on that.

With modern compilers you can rely on the opposite: that the compiler
will do the job much better you can, and the more information you give
the compiler to do that job, the better job it does.


I have parse error myself here. By making it inline when you think that there will be
specific benefit for the program if the function gets inline, you do give more information
to the compiler.


That's almost a given as of 2005, because few C++ compilers so far
do a decent "whole program optimization" which is needed for that.

Essentially that, for those hooked on needless optimization, one should
give the compiler the most information possible to do the very best job.

And that means preferably using 'inline' functions and in-class definitions.
:)

Lest I be misunderstood here: I'm not recommending that unconditionally,
only for the mindless micro-optimization-freaks.





The effect depends on everything. The complete program, the compiler,
system, options, everything. All you know is that _whatever_ you're asking
the compiler to optimize for, the more information you make available to it
(like the source code of functions), the better a job it will generally do.





Naturally: if the code doesn't need to be correct it can be infinitely
optimized! Great idea. Correctness, goodbye!





Yes, there I agree very much.





Nope. Or rather, anything's possible, but it's extremely unlikely. On
the contrary, if you the compiler to optimize for code space, it's much
more likely to do a perfect job if you give it maximum information.





Incorrect.



Major Error: I cant comprehend your points.

Aborted.
 
I

Ioannis Vranos

Alf said:
Parse error. I gather it's an attempt at phrasing a false dilemma? ;-)


In addition to the other things that I mentioned (that essentially boil down that the
points provided do not make much sense to me), in this way the compiler also has to parse
the same definitions more times than once, and thus making your above specific point of
having to parse an additional file(?!) having not much sense too.
 
A

Alf P. Steinbach

* Ioannis Vranos:
In addition to the other things that I mentioned (that essentially boil down that the
points provided do not make much sense to me), in this way the compiler also has to parse
the same definitions more times than once, and thus making your above specific point of
having to parse an additional file(?!) having not much sense too.

I think that may be because you're thinking too much (or perhaps only)
about optimization.
 
I

Ioannis Vranos

Alf said:
I think that may be because you're thinking too much (or perhaps only)
about optimization.


Actually you made an argument about compile-time optimisation or something. :)


It would help much, if you provided a summary table of your points regarding benefits that
inline provides other than inlining. :)
 
A

Alf P. Steinbach

The question discussed is

* Ioannis Vranos:
why would someone want to place a function definition in a header file?


from which followed

* Ioannis Vranos:
Meaning, documentation is an entirely other subject, and it has not anything to do with
our subject. It is a matter of what documentation style one wants, and documentation tools.

Yes, it is a matter of documentation style, and no, that doesn't mean it
has nothing to do with the subject.

Sorry but I can't understand this. For me the function definition remains the same
"self-documenting" (whatever this means) when you place it in one implementation file only.

With the definition in the header file you need only look at the header
file.

With the definition in a separate implementation file you may need to look
in two files, and the comment(s) in the header file may be inconsistent with
the separate implementation.

For some functions that is a good reason for some programmers to put the
definition in the header file.

The first time. In the case of inlined functions however, the second time
you will have to recompile *all* the other files that make use of the function.

Yes, and (1) there's no need to remind me of what I have written by
reflecting it back in distorted form, and (2) there is a difference between
a file being compiled and a necessery compiler invocation; the latter can be
a cost both in build time (which I suspect is the only aspect you've
considered?) and in convenience and programmer effort.

Did you understand this now?

It is not primarily about build or program execution efficiency.

You place the declaration only once, inside the same header that you would place the
redundant inlined definition, and include it as you would do in your (strange) inlining
approach. And you avoid the recompile-all when you modify the function definition.

Sorry, I can't figure out what you mean here.

I think the parser is buggy, and not the data itself. :)

It would be nice if you attempted to present the data in a more
understandable way, so that this buggy parser can cope with it.


[
Please don't cut quoting so that what remains is without meaning.

Reinserted:

Ioannis:
"it will inline whenever it can for the ones that you defined as inline and
have placed in scope before the function use in a translation unit"
]
I have parse error myself here. By making it inline when you think that there will be
specific benefit for the program if the function gets inline, you do give more information
to the compiler.

No, making something inline is not necessarily about "when you think there
will be ... a benefit ... if [it's inlined]".

That is, by the way, invalid reasoning in more than one way. Consider your
reasoning here: "[the compiler] will inline [machine code] wherever it can
because you're making [the source code] [textually] inline where you think
there will be a benefit if the machine code is inlined." The compiler is
not guessing your intentions, and your intentions are not e.g. mine.

Presumably the parse error is simply because you cut away the statement that
"that is incorrect" referred to.

...

Major Error: I cant comprehend your points.

Aborted.

Try:

* Better quoting, with more context, less irrelevant quoted, fewer
spurious line breaks, fewer empty lines, fewer very long lines.

* Thinking about OTHER things than optimization.

Cheers, and hope this helps,

- Alf
 
I

Ioannis Vranos

Alf said:
Sorry, I can't figure out what you mean here.


--somefunc.h

void somefunc();



--somefunc.cpp

#include "somefunc.h"

void somefunc()
{
// ...
}



--someotherfile.cpp

#include "somefunc.h"

// ...
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top