Splitting template classes between .h and .cpp files

This is a discussion on Splitting template classes between .h and .cpp files within the C++ Programming forums, part of the General Programming Boards category; What is your preferred way of splitting template classes into a .h and a .cpp file, as is normal with ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Splitting template classes between .h and .cpp files

    What is your preferred way of splitting template classes into a .h and a .cpp file, as is normal with regular classes?

    Do you just #include the .cpp file at the bottom of the .h file so the definition and implementation are both in the .h file ready to include to main.cpp or whatever, or do you use some other method? In that case which?

    I have always been told that #include'ing .cpp files was bad practice, so which method makes for the best code? Lets get some opinions on this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    You cannot split templates into multiple files.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Walle View Post
    You cannot split templates into multiple files.
    That was a bit of a blunt answer, no? In theory i guess you are right but in practice you can put the implementation into it's own .cpp file and have the preprocessor move the contents into the .h file before it reaches the compiler.
    So i guess what i'm asking is; is this a better approach than just putting all the code in the .h file yourself?
    I think it's more consistent, the template classes don't stick out from the rest by not having the implementations in a seperate .cpp file. I was simply requesting some opinions on the matter, how is this problem usually tackled? Does everyone just dump the code in the .h file?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Walle
    You cannot split templates into multiple files.
    That is false. You can split templates into multiple files -- the problem is that all the files must be included somehow.

    This link explains the situation in more detail. Until compilers start supporting the "export" keyword (which may never happen), you will be stuck with including the template implementation.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    I'm sorry, I didn't intend to be a wise-ass. I thought I could trust a source such as cplusplus.com, from where I learned that templates can't be split into multiple files. A quote from their tutorial, which i read a few weeks ago:
    Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
    From this page: Templates

    Ofc, using my own logical thinking, I should have thought about the possibility to use the preprocessor to move the code around. As I said, I'm sorry, didn't intend to be a wise-ass.

    Btw, it made me wonder what else I cannot trust that I read on cplusplus.com...

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    19
    Quote Originally Posted by bithub View Post
    That is false. You can split templates into multiple files -- the problem is that all the files must be included somehow.

    This link explains the situation in more detail. Until compilers start supporting the "export" keyword (which may never happen), you will be stuck with including the template implementation.
    That is irony. I have been reading that FAQ thoroughly the last few days, I just haven't got to that section yet...As I said, I'm sorry about the misinformation. Thought I could trust cplusplus.com..

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,050
    Personally I like to put regular header stuff in a .h file, and but template implementation in a seperate .hpp file that is included by the .h file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Neo1 View Post
    Do you just #include the .cpp file at the bottom of the .h file so the definition and implementation are both in the .h file ready to include to main.cpp or whatever
    I've done it that way before. Feels dirty, but gets the job done. Normally though, I just jam everything into a single header file.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    In almost all cases people put it all in a header file. This is the normal thing to do for templates.
    The main execption to this is if you only ever intend to use the template inside one cpp file and you can then just dump it all inside that cpp file somewhere above where it is used. That's sometimes just laziness though, and they do this in the article you linked.

    Also, if you read it again carefully, especially the last few paragraphs, you'll notice that they don't say anything about splitting template code between header and source files at all.
    Last edited by iMalc; 01-14-2010 at 12:10 AM.
    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"

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I've done it that way before. Feels dirty, but gets the job done. Normally though, I just jam everything into a single header file.
    Ditto that. I've done both and prefer just jamming it all in the header. Either way feels dirty but at least in the header it feels a bit less dirty.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    When a template is big enough that I don't want to put it all in one header (and some things, like a geospatial index tree, can get very large), I usually put the implementation code in a .ipp file and include that at the bottom of the .hpp file containing the declaration. Sometimes I instead put the implementation in a .hpp file in an impl subdirectory.

    The trick is teaching VS to recognize .ipp as source code. I always forget how to do it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I think it's in in tools->options->text editor
    It let's you define different extensions that will still map to use the C++ compiler.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    I always put them inside a header and make them inline - ie write the code directly in the definition. I can always use code folding to only see the definitions, and it solves a lot of pain having to duplicate template parameters and friends and stuff when working with templates with separate definition and implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Get .cpp and .h files to communicate...
    By yaya in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 11:45 PM
  3. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 05:37 PM
  4. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM
  5. Class files (.h and .cpp
    By Todd in forum C++ Programming
    Replies: 7
    Last Post: 02-14-2002, 02:07 PM

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