Thread: If I want to implement a template class, must I implement it in a header file?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    If I want to implement a template class, must I implement it in a header file?

    If I want to implement a template class A.

    FileA.h
    Code:
    #include <FileA.h>
    template <typename handler>
    class A {
    A(handler*);
    ...
    };
    ---------------------------
    FileA_impl.h
    Code:
    template<typename handler>
    A<handler>::A(handler * hd):hd(0){}
    ...
    ---------------------------

    I can't name FileA_impl.cc here, can I?
    Last edited by meili100; 11-12-2007 at 02:38 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can name it whatever you want, but you must treat it like a header (some people use .inl to indicate it is inlined source code). That means making sure it gets included by source files that use class A.

    You should probably #include "FileA_impl.h" at the bottom of FileA.h, since any source file including the first will also probably need the second.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I've never seen the point in separating template class implementations from their definitions. I usually write the code directly in the class definition, i.e. in the .h file.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you. Can I #include "FileA_impl.h" at the top of FileA.h?

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by brewbuck View Post
    I've never seen the point in separating template class implementations from their definitions. I usually write the code directly in the class definition, i.e. in the .h file.
    This may cause significant code bloat, meaning your executable size may increase dramatically

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This may cause significant code bloat, meaning your executable size may increase dramatically
    I do not think so. The executable code bloat would happen whether or not you try and separate the template declarations from their definitions. Note that including the definition file in the header file is equivalent to just placing everything in the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by laserlight View Post
    I do not think so. The executable code bloat would happen whether or not you try and separate the template declarations from their definitions. Note that including the definition file in the header file is equivalent to just placing everything in the header file.
    Thank you. Let me summarize the points: If I include the impl.h in the FileA.h, whether in the front (as in my original post), or in the bottom, the compiler won't complain. And in both cases it's just like "inline" the implementation.

    Please correct me if I am wrong. I also want to make sure that I can include FileA_impl.h in the front.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't think you can do it at the top. It needs to be at the bottom because the declaration of the class in the original FileA.h needs to come before the definitions of its member functions in FileA_impl.h.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by meili100 View Post
    This may cause significant code bloat, meaning your executable size may increase dramatically
    Not in my experience. Templates are not instantiated until they are used. In fact, only the parts of the template which actually get used get instantiated. There is no difference (that I can find) between writing the method code directly inline or including it later in the template header file. The compiler makes its own decisions which functions to inline, anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. simple class with header file
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2001, 08:14 PM