-
April 12th, 2010, 12:28 PM
#1
Inline templates
Hi for all
I would like to know why templates in the most of cases are inline. And other thing, what is the real advantage in inline templates? templates class methods aren't always little enough to be template
-
April 12th, 2010, 12:32 PM
#2
Re: Inline templates
Originally Posted by C#er
I would like to know why templates in the most of cases are inline.
I did not know the statistics, actually
Originally Posted by C#er
what is the real advantage in inline templates?
The same as the real advantage (and disadvantage) of inline functions in general.
Originally Posted by C#er
Is inline templates reasonable even if some of my methods are a little bit big?
The compiler always has the right to decline your suggestion to inline, but then if you feel that it is not a good candidate for inlining, why suggest that it be inlined?
-
April 12th, 2010, 01:00 PM
#3
Re: Inline templates
I've read this explanation:
Templates and inline function definitions are the 'exception' to the one-definition-rule because they have to be available to the compiler where they are used. A compiler could not ever inline a function if it doesn't know what the function looks like.
in this link:
http://www.gamedev.net/community/for...opic_id=535329
But it doesn't explain the inline use.
-
April 12th, 2010, 01:08 PM
#4
Re: Inline templates
Basically, the idea is to avoid function call overhead by integrating the generated code for the function into the generated code of the call site. This comes at the cost of repeating the code generated for the function, should the function call appear in more than one place, which is why inline functions are expected to be rather small.
-
April 12th, 2010, 01:55 PM
#5
Re: Inline templates
Originally Posted by C#er
I would like to know why templates in the most of cases are inline. And other thing, what is the real advantage in inline templates? templates class methods aren't always little enough to be template
I think you are confusing different things.
If you have a template class or function, you need to provide the definition of the (member) function in the header file (or a file included in the header file) and not in a separate .cpp file, because the function will only be compiled when the template is instantiated. If you would put the function definitions in a .cpp file, you would get linker errors, because the functions are actually never compiled.
Inlining functions is a different topic. It's mostly about performance.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 12th, 2010, 02:22 PM
#6
Re: Inline templates
Yes. My template class definitions are always like this:
Code:
template<typename T>
class SomeClass
{
...
};
#include "SomeClass.inl"
Where "SomeClass.inl" is the file where the inline methods are defined. I never use a cpp file.
-
April 12th, 2010, 02:37 PM
#7
Re: Inline templates
Originally Posted by D_Drmmr
I think you are confusing different things.
If you have a template class or function, you need to provide the definition of the (member) function in the header file (or a file included in the header file) and not in a separate .cpp file, because the function will only be compiled when the template is instantiated. If you would put the function definitions in a .cpp file, you would get linker errors, because the functions are actually never compiled.
Inlining functions is a different topic. It's mostly about performance.
Does the keyword inline even serve any purpose any more? I figured it's still only used to remind the user he is manipulating a very small function.
I mean, take g++ for example. Does it in any way take your inline keyword into account in any way?
-
April 12th, 2010, 03:45 PM
#8
Re: Inline templates
Originally Posted by monarch_dodra
Does the keyword inline even serve any purpose any more? I figured it's still only used to remind the user he is manipulating a very small function.
I mean, take g++ for example. Does it in any way take your inline keyword into account in any way?
Did you try compiling a small example and analyze the assembly code? You can inline whatever function that you want. Some compilers may include a switch that forces the inlining of functions. Size doesn't matter.
-
April 13th, 2010, 05:17 AM
#9
Re: Inline templates
Originally Posted by monarch_dodra
Does the keyword inline even serve any purpose any more? (...) Does it in any way take your inline keyword into account in any way?
Whether or not the function is actually inlined, the inline keyword does make a difference: it allows the function to be defined in a header file that is included in more than one translation unit without causing multiple definition errors.
-
April 13th, 2010, 06:34 AM
#10
Re: Inline templates
Originally Posted by laserlight
Whether or not the function is actually inlined, the inline keyword does make a difference: it allows the function to be defined in a header file that is included in more than one translation unit without causing multiple definition errors.
Of course.
i was merely asking because I never actually write it unless it is needed for the above reason. I know the compiler will inline the ones I know are obvious (get/set), it will not inline the complicated one, and will probably know better than me what to do with the "intermediate" functions
-
April 13th, 2010, 10:25 AM
#11
Re: Inline templates
Originally Posted by monarch_dodra
Of course.
i was merely asking because I never actually write it unless it is needed for the above reason. I know the compiler will inline the ones I know are obvious (get/set), it will not inline the complicated one, and will probably know better than me what to do with the "intermediate" functions
No offense, but that is a bad assumption. I have made the same mistake before and I have seen the compiler inline rather large functions before. I really don't know what causes a compiler to determine what should be inlined. Inlining a large function may increase the size of the executable but if the function is only called in a few places and is utilized at high rates inlining a large function may actually serve a purpose. I'm not stating an opinion about whether large functions should or shouldn't be inlined. I'm only stating an observation that I have made in the past. Personally I think that the compiler should inline all functions requested by the user. If the user makes a dumb decision and the performance is degraded then they can simply undo it and try something else. In cases like this I don't want to have to guess when the compiler will or won't inline a function.
In another note, I am not sure if this thread was really about inlining at all. Perhaps the OP was talking about template functions always being within the header file which does not mean that they are inline functions.
-
April 13th, 2010, 10:47 AM
#12
Re: Inline templates
Originally Posted by kempofighter
No offense, but that is a bad assumption. I have made the same mistake before and I have seen the compiler inline rather large functions before. I really don't know what causes a compiler to determine what should be inlined. Inlining a large function may increase the size of the executable but if the function is only called in a few places and is utilized at high rates inlining a large function may actually serve a purpose. I'm not stating an opinion about whether large functions should or shouldn't be inlined. I'm only stating an observation that I have made in the past. Personally I think that the compiler should inline all functions requested by the user. If the user makes a dumb decision and the performance is degraded then they can simply undo it and try something else. In cases like this I don't want to have to guess when the compiler will or won't inline a function.
In another note, I am not sure if this thread was really about inlining at all. Perhaps the OP was talking about template functions always being within the header file which does not mean that they are inline functions.
No offense taken, since assumption != opinion.
The compiler can only choose to not make something inline. If you declared your function inside the cpp, he doesn't have a choice. But at the same time, all functions implemented inside your .h are either have the inline keyword, or are implicitly inlined by in-class declaration. If you enforce "inline keyword means inline", then basically you have taken all control out of the compiler.
Maybe (probably) you are right, but my view is that pondering on if a function should or should not be inlined takes too much effort to optimize, and I want to concentrate on more important stuff. I let my compiler handle it. I trust him.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|