3.4.1 Solution 1

The first solution is to change test.cc to include the class template definition, as illustrated in listing 6.


Listing 6:templateclassinstantiation
 
1// this is a test program, call it test.cc 
2#include ”ll.h” 
3#include ”ll.cc” 
4 
5int main(void
6{ 
7  LinkedList<int> *pLI; 
8 
9  pLI = new LinkedList<int>(23); 
10  return 0; 
11}

This approach works! However, it has a potential problem. What if client001.cc, client002.cc,... client100.cc all include references to LinkedList<int>? Each individual client source code file can include ll.cc, but then each object file (client???.o) also contains an instance of LinkedList<int>.

Many linkers, including ld (which is implicitly used by g++), recognizes the duplicate template class method definitions, and folds all duplicate copies into one in the executable file.

The method works, but it can potentially lead to unnecessary compile time (due to the emission of template class instance code). In other words, it appears to work fine for smaller projects, but the overhead is impractical for large projects.