Issues in the instantiation of template classes
Issues in the instantiation of template classes
Issues in the instantiation of template classes
Abstract: Teaching students to incorporate template classes into their C++ projects is an important concept in object-
oriented programming. The most efficient implementation method for template classes is dependent on several factors.
Two significant factors are different integrated development environments with differing requirements, and
incongruous philosophies among instructors. Herein, several template class instantiation methods under Gnu compilers
will be discussed, along with their pros and cons.
Category and Subject Descriptors: D.1.5 [Programming Techniques]: Object-oriented Programming; D.3.2
[Programming Languages]: Language Classifications – object-oriented languages: C++; D.3.3 [Programming
Languages]: Language Constructs and Features – classes and objects; K.3.2 [Computers and Education]:
Computer and Information Science Education — computer science education
General Terms: Languages, Performance, Standardization
Keywords: C++, template class, gnu compiler, implicit instantiation, explicit instantiation, header file, implementation
file, Borland Model.
class in each unit that declares it, with the linker handling explicit instantiation that can be employed when a C++
resolution. If several compilable units each declare template class is declared in two files. Each method is
identical instances of a template class, the compiler will followed by an analysis of its pros and cons. The three
create an instance within the object code for each unit, methods that will be discussed are:
representing a duplication of effort on the compiler’s part. o Place explicit instantiation(s) at the end of the cpp
Flags can be used to alter this behavior and eliminate file
the creation of duplicate instances by the compiler. A o Import the class’s implementation file into the
model denoted the Cfront Model in [2], works with the class’s header file or into the application file
same goal as [4] envisioned by creating a repository of o Place explicit template instantiations in a separate
template instances and is activated by using the -frepo flag file
in g++. This methodology is named for an AT&T C++ The first two require no special handling, while the third
translator that used a repository of template instantiations. will be accomplished via a makefile.
The -fno-implicit-templates g++ flag suppresses
implicit instantiation and requires the programmer to 3.1 Place Explicit Instantiation(s) at the End of the
explicitly instantiate declared instances. The -fno-implicit- Implementation File
templates flag is ordinarily used with outside linkers, After reading the implementations of member functions in
although it can be used with the Gnu linker. If used with the template class’s implementation (cpp) file, the compiler
the Gnu linker all template class instances that will be has processed the entire class. At this time it possesses the
declared must be explicitly instantiated before the linking ability to generate code for concrete instances of the class.
phase. To accomplish this, explicit declaration of those instances
An underlying assumption in all the papers described can occur before the compiler completes processing of the
is that the template class declaration, including function template class’s cpp file. For example in a template array
implementations, is assumed to be placed entirely inside a class, commonly presented as an example of template
header file. This leads to the question: Is declaration of a classes, a set of explicit instantiations can be placed at the
template class entirely in a header file, or simulating the end of the class’s implementation file. An example of such
same, the proper way to teach and apply template classes? instantiations is shown in Figure 1. Note the use of the
template keyword.
3. METHODS OF INSTANTIATION OF
#include “MyClass.h”
INSTANCES OF TEMPLATE CLASSES IN TWO
FILES template class Array<int>;
It can be suggested that template classes be defined and template class Array<MyClass>;
have their member function implementations entirely
template ostream &operator<<
within a header file because code generation for a concrete (ostream &out, const Array<int> &right);
instance of a template class can only occur after the
compiler has processed a template class in its entirety. If an
application imports the header of a template class, code
Figure 1: Example of explicit instantiation
generation can occur when an instance is declared in the
application only if the complete class is in its header. If the
The instantiation of the Array<int> is ordinary, but to
class is specified using the two-file approach common in
instantiate an Array with elements of, for example,
C++, the compiler will not have access to the code of the
MyClass in Figure 1, the header file for that class must be
member function implementations. As a result, implicit
imported. Also, note the instantiation of the << operator.
code generation is not possible when an instance
Since stream operators can’t be class members,
declaration is encountered. If some technique isn’t applied
instantiation of a template class won’t instantiate its stream
before linking, the linker will not find any code for the
members. Therefore, the stream members must be
concrete instances and will produce undefined symbol
explicitly instantiated, too.
errors.
The advantages of this method apply to all true two-
As noted, C++ classes are traditionally declared in two
file template class implementations. First, code for
files. This is accomplished using a header file with file
functions in the class is compiled one time only. For units
type h that contains the actual class declaration and an
that import its header, the compiler only processes the
implementation file with file type cpp that contains the
declarations. Second, the class, unlike classes entirely in a
code for the member functions declared in the header file.
header, can be compiled stand-alone. Header files can’t be
This is used for non-template classes because it is a cleaner
compiled with Gnu compilers.
approach as opposed to including the implementation in the
The disadvantage of this method is that it defeats the
header file, which is generally used for items that don’t
main purpose of a template class. In the same file with the
allocate storage, such as declarations. This document
generic definitions of the member functions, the concrete
presents three methods (there are certainly others) of
type(s)/value(s) to be used must be specified. As shown in 3.3 Place Explicit Template Instantiations in a
Figure 1, the cpp file would need to include header files if Separate File
there are any references to nonstandard types. These A makefile can be employed for just about any task on a
characteristics are in direct contradiction to the generic Unix machine. Explicit instantiations like those in Figure 1
nature of a template class. Students pick up on these are placed into a separate file called a types.tpp. The
problems quite rapidly. As a result, this method could tend makefile for the project can be used to construct a
to discourage students from employing the useful concept temporary file containing the template class’s
of template classes. implementation (cpp) file followed by the instantiations in
types.tpp. This new temporary file would then contain the
3.2 Import the Class’s Implementation File definitions and explicit instantiations like the one described
There are two commonly applied methods of importing a in the first method discussed. This new temporary file
template class’s implementation file. Either the class’s would be used to create the object code for the class.
header file imports, or the application imports both the Figure 2 shows the makefile from [8] that creates the object
header and implementation files, in that order. code file (makefile target Array.o) by, in order:
1. copying the class’s cpp file to temporary file
3.2.1 Include the Class’s Implementation File in the temp.cpp
Class’s Header File 2. appending the explicit instantiations (in types.tpp)
This method represents an attempt to have what can be 3. compiling the temporary file
perceived as the best of both the two file and single file 4. copying the resulting file temp.o to Array.o
template class implementations. At the end of the header 5. deleting the temporary file.
file, the implementations are imported, typically by If debugging, the temporary file must be retained, as the
including the class’s implementation file. Using this debug info in Array.o refers to it.
method, the class is actually processed in its entirety when The advantages are the same as those for the previous
imported, akin to when the class appears in its entirety in a methods. It also has an advantage over the second method
header file, but can still be compiled stand-alone. by adhering to the concept of never teaching students to
Importation of the implementation file into the header file include cpp files. The application will simply include the
(note that this occurs above the #endif) was occasionally
Debug =-g
detailed in literature during the late 1990’s.
test: testArray.o Array.o
3.2.2 Include the Class’s Implementation File in the # Create executable
Application File g++ -o test testArray.o Array.o $(Debug)
This method is similar to the previous method but the testArray.o: testArray.cpp Array.h
application file will include both the class’s header and # Create object code
implementation files. This method also processes the class g++ -c testArray.cpp $(Debug)
in its entirety when it is included and is detailed in various
literatures including [7]. Array.o: Array.cpp Array.h
There are two advantages of these methods. The first # Make a temporary copy
advantage is that they perform implicit instantiation of cp Array.cpp temp.cpp
template classes since, when imported, the compiler # append the explicit instantiations
# to the temporary file
processes the entire class and thus possesses the ability to cat types.tpp >> temp.cpp
generate code. Secondly, the class may be compiled on its # compile the compendium
own. Explicit instantiation isn’t required with this method. g++ -c temp.cpp $(Debug)
# Make this the object code for the array;
#now has all instantiations
There is also a disadvantage to these methods, mv temp.o Array.o
although it is conceptual in nature. The concept of # Remove the temporary
importation of cpp files is dangerous. These files don’t \rm -f temp.cpp
typically have the compiler directives to prevent multiple
inclusion, and importation under these circumstances is, in Figure 2. Method of using a makefile to construct temporary file for explicit
general, a poor practice. This can be handled by placing instantiation
compiler directives in the cpp file. If students are shown
this method, they may become tempted to use it in header file.
situations involving non-template classes, where The disadvantage of this method is in the overhead. A
importation of a cpp file is inappropriate. user of the Array class must create the file of explicit
instantiations for any new application and be sure the
makefile corresponds to the name of the file containing
the explicit instantiations.
REFERENCES
[1] Cole, M. J. and Parker, S. G., Dynamic compilation of C++ template code, Scientific Programming 11, pp-321–327, 2003.
[2] GNU Project, A GNU Manual, http://gcc.gnu.org/onlinedocs/gcc/Template-Instantiation.html, Section 6.5, 2005.
[3] Gregor, D. and Jarvi, J., Object oriented programming languages and systems: Variadic templates for C++, Proceedings of the 2007
ACM symposium on Applied computing SAC '07, pp-1101-1108, 2007.
[4] McCluskey, G. and Murray, R. B., Template instantiation for C++, ACM SIGPLAN Notices, Volume 27 Issue 12, pp-47-56, 1992.
[5] Onyshkevych, B., Information extraction task: Template design for information extraction, Proceedings of the 5th conference on
Message understanding MUC5 '93, pp-19-23, Association for Computational Linguistics, 1993.
[6] Sanchez, A. and Jia, D. W., Towards a Graphical Notation to Express the C++ Template Instantiation Process, Addendum to the 2000
proceedings of the conference on Object-oriented programming, systems, languages, and applications (Addendum) OOPSLA '00, pp-
117-118, 2000.
[7] Savitch, W., Absolute C++, 1st Edition, Addison-Wesley Longman Publishing Co., Inc., pp-672-677, Boston, MA, 2002
[8] Spiegel, D. S., Array Template Class Example, CSC 136, Computer Science II Example,
http://faculty.kutztown.edu/spiegel/CSc136/C++/Array Template ADT/2 File/Gnu_Types.tpp/ , 2005.