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

This is a discussion on If I want to implement a template class, must I implement it in a header file? within the C++ Programming forums, part of the General Programming Boards category; If I want to implement a template class A. FileA.h Code: #include <FileA.h> template <typename handler> class A { A(handler*); ...

  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 01:38 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    21,743
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,319
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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, 11:21 AM
  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, 09:46 AM
  4. simple class with header file
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2001, 08:14 PM

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