Need hints on templates

This is a discussion on Need hints on templates within the C++ Programming forums, part of the General Programming Boards category; Hi, I usually separate template class definitions in 3 files, a .h for declaration, a .cpp for method definition and ...

  1. #1
    Lode Runner
    Join Date
    May 2004
    Posts
    53

    Need hints on templates

    Hi,

    I usually separate template class definitions in 3 files, a .h for declaration, a .cpp for method definition and a .hpp for "template-dependent" method definitions. Standard procedure.

    I understand its best to keep the .hpp file to a minimum (to avoid code bloating, long preprocess times, etc.). Bu I'm lazy so I usually just put everything in the .cpp file to start with, and then move the definitions to the .hpp every time there's a compilation error.

    My question is this:
    How am I going to know in advance what I have to put in the .hpp file?
    I understand what a compilation unit is, and I understand why some definitions are needed at compile-time, but I can't seem to apply theory to actual cases.
    Could someone break it down the simple way for me?

    Secondary question:
    What's an official terminology for those methods that need to be defined in the .hpp file. I don't like that "template-dependent" I used earlier

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    Your procedure sounds a little strange to me. If you are going to fix the compile errors by moving the class template's function definitions from the source file to an auxiliary header, then you might as well do without the source file and just put all the function definitions in the auxiliary header.

    If you do not want to do this in order "to avoid code bloating, long preprocess times, etc.", read the answer to this FAQ: How can I avoid linker errors with my template classes?
    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
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    Quote Originally Posted by laserlight View Post
    Your procedure sounds a little strange to me. If you are going to fix the compile errors by moving the class template's function definitions from the source file to an auxiliary header, then you might as well do without the source file and just put all the function definitions in the auxiliary header.
    Sure, that works too.
    I'm familiar with the method presented by Marshall Kline, too.
    I'm just trying to understand what needs to go in the auxiliary header and what can be in the source file, if I want to do things that way.
    Call it curiosity, I just want to understand C++ better instead of just "making it work".

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by krappa View Post
    Sure, that works too.
    I'm familiar with the method presented by Marshall Kline, too.
    I'm just trying to understand what needs to go in the auxiliary header and what can be in the source file, if I want to do things that way.
    Call it curiosity, I just want to understand C++ better instead of just "making it work".
    If it's got a template in it, it needs to go in the auxiliary header. Template code is not generated unless it is instantiated (until that far-off day when "export" works, or we all start using Comeau), so any source that's just a bunch of template classes/functions/etc that doesn't actually use those classes/functions/etc won't actually generate any code.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,990
    Quote Originally Posted by krappa
    I'm just trying to understand what needs to go in the auxiliary header and what can be in the source file, if I want to do things that way.
    All non-template function definitions can go in the source file. All function template definitions, as well as definitions of member functions belonging to class templates, would go in the auxiliary header. Kline somewhat answered this in Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 12:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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