0

I have been looking around for a while to see if there is a way to solve the issue of separating template functions to have a declaration in a header file (.h) and an implementation in a source file (.cpp).

I haven't been able to solve this issue, it seems like this is something pretty trivial that would have to have been solved in the past. But I am just unable to find it, which is why I am posting here.

Note, I am not making my entire class a template class. I just want to template certain functions in my class. If anyone has any information on this I would be greatly appreciative. Thank you in advance for your help an consideration.

b4hand
  • 9,550
  • 4
  • 44
  • 49
BigBerger
  • 1,765
  • 4
  • 23
  • 43
  • Try including the cpp file in the header file at the end of the header file. But otherwise, it's not possible. – Jagannath Mar 31 '15 at 23:50
  • @Jagannath That doesn't really solve the core issue. – Neil Kirk Mar 31 '15 at 23:51
  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Neil Kirk Mar 31 '15 at 23:51
  • A member function template is not much different from a function template. You always need their full definition when they are instantiated. It isn't pretty but is by design. – pmr Mar 31 '15 at 23:56
  • There have been efforts to introduce some kind of "separate compilation" model for templates, however they have failed and have been abandoned (export template). For fundamental reasons, you won't get the same benefits for "separately compiled" function templates as you do get for non-template functions. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf – dyp Mar 31 '15 at 23:56
  • @Unmerciful Turtle If you know the types in advance, then you can force the instantiation for those types. Otherwise, you just have to implement the functions in the header itself. – Jagannath Apr 01 '15 at 04:18

0 Answers0