|
CSE Home | CSE 326 Home | About Us | Search | Contact Info |
Henry Kautz, Fall 2000
A confusing issue with templates is understanding how and when the
machine code for a particular instance of the template is actually
generated. The default behavior of the g++ compiler when
compiling a file is to generate code for any use of a template
mentioned in that file. For example, if the file
MyModule.cc
contains the line
template class Stack<int> myStack;
then MyModule.o
will contain all the code for the
<int>
instantiation of the Stack
class. This has two bad effects:
Stack.cc
must be included in
MyModule.cc
, as well as the header
Stack.h
. (Sometimes this is done by having
Stack.h
include Stack.cc
; most of the
downloadable code supplied from the Weiss textbook web site does
this.)
The solution is to include the .h
file in all of the
.cc
files using the class, and to
instantiate the template in only one compiled
.cc
file. This is done as follows:
In your ordinary source files include only the header
.h
files for the templates used (and be sure those
.h
files do not include the corresponding
.cc
files). Then compile those files to object files
using a compiler flag -fno-implicit-templates
that
prevents the default automatic code generation behavior. For
example:
g++ -fno-implicit-templates -c MyModule.cc
Then create one or more special source files that explicitly
instantiate the templates that are needed anywhere in your
program. This file should include both the header .h
and body .cc
of each such template. A template is
explicitly instantiated by mentioning it without specifying
any variables of that type. For example, such a special file
MyInstantions.cc
could be:
#include "Stack.h"
#include "Stack.cc"
template class Stack<int>;
The Makefile for the example just given could be the following:
MyTest: MyModule1.o MyModule2.o MyInstantions.o
g++ MyModule1.o MyModule2.o MyInstantiations.o -o MyTest
MyModule1.o: MyModule1.cc Stack.h
g++ -fno-implicit-templates -c MyModule1.cc
MyModule2.o: MyModule2.cc Stack.h
g++ -fno-implicit-templates -c MyModule2.cc
MyInstantiations.o: MyInstantiations.cc Stack.h Stack.cc
g++ -fno-implicit-templates -c MyInstantiations.cc
The most important factors for good code are:
Department of Computer Science & Engineering University of Washington Box 352350 Seattle, WA 98195-2350 (206) 543-1695 voice, (206) 543-2969 FAX [comments to owner-cse326] |