I have a templated library where all of my classes are explicitly instantiated and built into a static library. The library is structured like this:
repo/
| - include/
| - mylib/
| - myclass.hpp
| - implementation/
| - myclass.ipp
| - source/
| - mylib/
| - myclass.cpp
Inside myclass.hpp
will be a template class declaration as well as an inclusion of the definitions, while inside myclass.ipp
is the actual definition, as well as explicit instantiation declarations. myclass.cpp
contains the actual explicit instantiations. They each look something like the following:
myclass.hpp
template <typename T>
class MyClass { ... };
#include "implementation/myclass.ipp"
myclass.ipp
template <typename T>
void MyClass<T>::MyClass() { ... };
...
extern template class MyClass<MyOtherType1>;
extern template class MyClass<MyOtherType2>;
...
myclass.cpp
template class MyClass<MyOtherType1>;
template class MyClass<MyOtherType2>;
NOTE: Yes, I am aware for what I’ve written here, this is not necessary. My actual library has much more complex templating, constraints, and specializations. This example is just for illustrating the layout of the code. I initially separated the declarations and implementations just to keep the headers readable, but started explicitly instantiated all of my classes because they are only valid for ~6 specific types I know beforehand.
Everything works great, I can build it as a static library and everything works as expected, except for one part. Because each header (.hpp
)directly includes its corresponding implementation file (.ipp
), all of the third party includes are transitively included for any user of the library. This makes it a pain to use the library when it is installed on a system that may not have a specific library, even when that library is statically linked to my library and should not be an issue.
Normally, this is just something you’d have to deal with when working with template libraries, but because my library is fully explicitly instantiated, I believe that the template definitions are not required anymore. I believe that I could distributed the headers, while somehow ignoring the #include "_.ipp"
part.
One thing I think I’d have to change is to move the explicit instantiation declarations into the headers, something like:
template <typename T>
class MyClass { ... };
#include "implementation/myclass.ipp"
extern template class MyClass<MyOtherType1>;
extern template class MyClass<MyOtherType2>;
But I am unsure how to reliably make sure that #include "_.ipp"
is included when the library is being developed, but to make sure it is never included when the library is distributed/installed, so that the headers can just be used alone (along with the pre-compiled library).
Is this safe to do with how I have things structured? Again things are slightly more complicated, but I think that the outline I’ve shared here should be fairly representative of the separation I have between the .hpp
, .ipp
, and .cpp
files.