I just came across the fact that templates can not conform to the separate compilation model (as in they cannot be separated into .h and .cpp files). The two solutions I can find on the internet are as follows:
Either add the implementation inside the .h file or (forgive me for saying this!) including the .cpp file.
Include multiple implementation versions for the same function with different data types. For example in the header file:
template<class T> void function(T val);
and in the implementation file:
void function<int>(int val){ //do stuff for int input }
void function<char>(char val){ //do stuff for char input }
for as many types as you can name.
But these solutions have the following disadvantages:
- The first is that the source code is view-able for any one using your library.
- The second is that you can not anticipate user defined types
I am trying to figure out how do library creators go about handling these issues? How do can they create generic data types, and hide their implementations?