CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    173

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inline templates

    Quote 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

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

    Quote 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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2005
    Posts
    173

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Inline templates

    Quote Originally Posted by C#er View Post
    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

  6. #6
    Join Date
    Oct 2005
    Posts
    173

    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.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Inline templates

    Quote Originally Posted by D_Drmmr View Post
    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?

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Inline templates

    Quote Originally Posted by monarch_dodra View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inline templates

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Inline templates

    Quote Originally Posted by laserlight View Post
    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

  11. #11
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Inline templates

    Quote Originally Posted by monarch_dodra View Post
    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.

  12. #12
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Inline templates

    Quote Originally Posted by kempofighter View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured