Thread: Template Header Shot my foot

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

    Template Header Shot my foot

    OK I've built up this complex set of classes initially using a lot of template meta programming. I did this to make the compiler do a lot of work beforehand for performance reasons.

    Well over time I decided that approach was a mistake. I really wanted more runtime polymorphism with my classes and not compile time polymorphism. So I went back and combined all my different types (e.g. real matrix complex matrix upper triangular etc.) into a single matrix type.

    That works great, but initially I was lazy and put all my function implementations inside my class. I could get away with it for templates, since they aren't created until used, and in fact the implementation is required to be in the header as far as I know.

    Unfortunately now I have an un-templated class implemented inside the header within the class declaration itself. This means I can only include the header into one .cpp source file unless I want linker errors.

    The header file is pretty complex and it would take quite some time to pull out all the implementations, generate the appropriate C++ syntax for it, and get the results I need in a timely fashion.

    Is there any possible way of making this work in a short period of time? The two things I've thought of are 1) including all my source in one ugly giant file, possibly via includes.
    and 2) slowly start to build a declaration only header for the components that I need. The latter approach ends up requiring the constant synchronization of two header files.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Mark everything with `inline'?

    Soma

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by SevenThunders View Post
    Unfortunately now I have an un-templated class implemented inside the header within the class declaration itself. This means I can only include the header into one .cpp source file unless I want linker errors.
    I'm not sure what you mean here. What link errors are you getting? You're using header guards aren't you?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Yes I have guards or perhaps it's a #pragma once for my compiler. My problem is this: If you have an implementation of a method or function inside of a header, you end up with multiple copies of that implementation for each file that includes your header.

    That leads to link time errors. I won't bother showing the exact error since I'm compiling my code via nvcc, the compiler and linker for the cuda development system.
    The link time errors are of course due to the fact that you can't have more than one implementation of the same function with the same signature, since it can't resolve the ambiguity.

    I just want to know if there are any tricks I can use to possibly get around this problem, other than the ugly solutions I mentioned.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    ^^ inlining would probably do the trick.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    OK I used some inlining and was able to extract a 'declaration' only version of my header in about an hour or so of work. I then just copied the original header into my .cpp source file.

    Not too bad, but guess what error I ran into when compiling some test code under visual studio 2008? Apparently no external references to the class methods in the .cpp source were found by the linker. All my implementations were still inside the class definitions but simply included in my main source file.

    Now here's where it get's weird. I pulled out just one constructor implementation, outside the class definition, the first one used.

    After recompiling that, all references were found, including other constructors and methods that were still defined inside the class definition. I'll have to try it with gcc to see if this carries over in any way. What is supposed to happen if you include one header that has all the implementations in one file, and then include into another file a header that only has declarations and function signatures? Shouldn't the linker still find your classes and methods?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function argument deduce
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2008, 08:56 PM
  2. Template overloading?
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 03:21 PM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM