Reputation: 10868
I'm working on a code base of template classes. It's header-only (no .cpp files). I would like to hide implementation and provide a shared library plus a couple of headers containing only declaration. Unfortunately sounds like doesn't make a sense. Since there is no compiled code, so what will be in such a shared lib? Trying to remove definitions from headers after compiling, causes undefined references. Is there a way to force compiler to ship objects in dll or shared library without having to explicitly instantiate template classes?
Upvotes: 4
Views: 2090
Reputation: 1
I also had a same problem.
Actually there is no way to seprate header and source file of a template class.
We can do it, but for that we have to include #include "source.spp"
the souce file itself into the header file, so that compiler can find the imlementation at compile time.
It makes no sense other than just easy maintanace. But then you have to proved .h as well as .cpp file to the client, so there would be an extra overhead.
I gave up and went with header only solution for this.
Upvotes: 0
Reputation: 59811
No, there isn't and wont be a way to do that for the foreseeable future. The only way to provide template C++ code is as header files only. Modules might change that, but that is unlikely to happen before your library is finished.
Something you can try is to split into implementation and explicitly instantiate all possible use cases. Then the library you ship wont work with any other types then the instantiated ones and would significantly reduce the benefit templates bring.
Upvotes: 6
Reputation: 8958
Template implementations need to be known at compile time. That's why you can't separate implementation from declaration. So if you want to have the advantages of templates, there is no way around passing your header(s).
Upvotes: 3