|
When the compiler generates a class, function or static data
members from a template, it is referred to as template
instantiation.
There are three methods of invoking template instantiation:
Explicit Instantiation
You request explicit instantiation by using the explicit template
instantiation syntax (as defined in the ANSI/ISO C++ International
Standard) in your source file.
You can request explicit instantiation of a particular template
class or a particular template function. In addition, member
functions and static data members of class templates may be
explicitly instantiated.
Explicit instantiation of a class instantiates all member functions
and static data members of that class, regardless of whether
or not they are used.
For example, following is a request to explicitly instantiate
the Table template class with char*:
template class Table<char*>;
When you specify an explicit instantiation, you are asking
the compiler to instantiate a template at the point of the
explicit instantiation in the translation unit in which it occurs.
Usage:
This might be useful when you are building a library for
distribution and want to create a set of compiler-generated
template specializations that you know will most commonly be used.
Then when an application is linked with this library, any of
these commonly used specializations need not be instantiated.
Another scenario might be a frequently used library that contains
a repository of template specializations for your development team.
Instantiating all such specializations in one, known translation unit
would allow easy maintenance when changes are needed and eliminate
cases of duplicate definition.
Performance:
Although time is required to analyze and design code for explicit
instantiation, compilation may be faster than for the equivalent
implicit instantiation.
Examples:
Following are the examples for explicit and implicit instantiation:
Class Template
Following are examples of explicit and implicit instantiation syntax for a
class template:
template <class T> class Array; // forward declaration for
// the Array class template
template <class T> class Array {/*...*/}; // definition of the
// Array class template
template class Array <int>; // request to explicitly
// instantiate Array<int>
// template class
Array <char> tc; // use of Array<char>
// template class which
// results in implicit
// instantiation
Function Template
Following are examples of explicit and implicit instantiation syntax for a function template:
template <class T> void sort(Array<T> &); // declaration for the sort()
// function template
template <class T> void sort(Array<T> &v) {/* ... */};
// definition of the sort()
// function template
template void sort<char> (Array <char>&); // request to explicitly
// instantiate the sort<char> ()
// template function
// Note: <char> is not
// required if the compiler
// can deduce this.
void foo() {
Array <int> ai;
sort(ai); // use of the sort<int> ()
} // template function which
// results in implicit
// instantiation
Command-Line Instantiation
By using a template option on the aCC command line, you can:
- Close a library or set of link units, to satisfy all unsatisfied instantiations without
creating duplicate instantiations.
- Specify what templates to instantiate for a given translation unit.
- Name and use template files in the same way as for the cfront based HP C++ compiler.
- Request verbose information about template processing.
Refer to Template Options for more information.
Compile-Time Instantiation
By default, compile-time instantiation is in effect. Instantiation is
attempted for any use of a template in the translation unit where the
instantiation is used.
All used template functions, all static data members and member
functions of instantiated template classes, and all explicit instantiations
are instantiated in the resulting object file.
If there are duplicate instantiations at link-time, the linker
arbitrarily selects an instantiation for inclusion in the
a.out or shared library.
The following command lines are equivalent; each
compiles a.C using compile-time instantiation.
aCC -c +inst_compiletime a.C
aCC -c a.C
Scope:
If your source code contains templates and you do not specify
any template command-line options nor explicit instantiations,
compile-time instantiation takes place for any use of a template.
If you specify a template command-line option, the option
takes precedence for all translation units on the command line.
Any explicit instantiation takes precedence over either a
command-line option or compile-time instantiation.
Usage:
Compared with developer-directed instantiation, compile-time
instantiation involves less coding time for the developer.
However, the design of your application may require the use
of some form of directed instantiation.
Why use Compile-Time Instantiation
- Compile-time instantiation is the default. It is easy to use.
- Your code may compile faster when using compile-time instantiation.
-
If your development environment uses a version control system
that is sensitive to file modifications, you may want to use the
current default, compile-time instantiation, to avoid major code
rebuilds.
|