C++ template method inlining

This is a discussion on C++ template method inlining within the C++ Programming forums, part of the General Programming Boards category; If you take the following example with all method implementation code inside the class definition. Code: template<typename T> class A ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    C++ template method inlining

    If you take the following example with all method implementation code inside the class definition.

    Code:
    template<typename T>
    class A
    {
    
    public:
      void SomeMethod()
      {
        ..
      }
    };
    then you also have the option to have it outside the class definition.

    Code:
    template<typename T>
    class A
    {
    
    public:
      void SomeMethod();
    };
    
    
    template<typename T>
    void SomeMethod<T>::SomeMethod()
    {
      ..
    }

    I've noticed that if you have the class definitions inside the class definitions the compiler (GCC, -O1 optimization used) creates a larger executable compared to when I have the methods outside the class definition. I assume this has to do with that the compiler inlines more methods. With highest optimization settings, the executables gets the same size.

    The version when the method definitions are inside the class is more verbose, especially when with many template parameters since do not have repeat it all over and over.

    My question if you have the methods inside the class definition, will you get penalized by excessive inlining or is the compiler still smart enough avoiding this?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    I cannot tell what code it adds, but -O1 does not include function inlining: Optimize Options - Using the GNU Compiler Collection (GCC)

    I would not care about the size that much and leave it all to the compiler (not to mention that compiler can inline functions in both cases).
    Last edited by kmdv; 08-02-2011 at 04:06 PM.
    I never put signature, but I decided to make an exception.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    IMHO, just trust the compiler. If it chose to inline the method call then it was probably a reasonable candidate. A few Kb either way in the exe size isn't going to matter.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,244
    Quote Originally Posted by TotalTurd View Post
    My question if you have the methods inside the class definition, will you get penalized by excessive inlining or is the compiler still smart enough avoiding this?
    That really depends on the compiler, and what it does in response to particular optimisation settings.

    In principle, virtually any form of inlining is a hint to the compiler, that the compiler is free to ignore. Similarly, if the compiler can see the function definition (implementation) then there is nothing preventing the compiler from inlining it. In fact, with some smart compilers or linkers, there is potential to inline functions that the compiler does not have visibility of.

    It all depends on implementation choices made by the developers of the compiler. As you have noted, g++ does different things depending on optimisation settings (with an occasional trade-off at higher optimisation settings between speed, executable size, correctness, and other factors - compilers and their optimisers can be quite complex bits of code). Other compilers may - and often do - behave differently.

    The C++ standard imposes very few constraints in this regard.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Depending on Template Method Return Type
    By M.Richard Tober in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2011, 09:34 PM
  2. Replies: 2
    Last Post: 08-18-2010, 03:24 PM
  3. template method
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2009, 09:17 PM
  4. template method
    By linuxdude in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 12:32 PM
  5. Why can't it recognize template method?
    By 6tr6tr in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2008, 10:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21