0

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?

Aelgawad
  • 184
  • 1
  • 16
  • Related: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – drescherjm Jun 30 '17 at 15:30
  • 2
    Possible duplicate: [How to hide an implementation helper template?](https://stackoverflow.com/questions/5869479/how-to-hide-an-implementation-helper-template) – NathanOliver Jun 30 '17 at 15:32

0 Answers0