1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
Class templates hi(class template: instantiation) are instantiated when an
object of a class template is defined. When a class template object is
defined or declared its template parameters must explicitly be specified.
Template parameters are em(also) specified when default template parameter
values are specified albeit that in that case the compiler provides the
defaults (cf. section ref(PARTIAL) where tt(double) is used as the default
type to use for the template's tt(DataType) parameter). The actual values or
types of template parameters are
hi(class template: deducing parameters)
em(never) deduced from arguments as is done with function template
parameters. So to define a tt(Matrix) of complex-valued elements, the
following syntax is used:
verb( Matrix<3, 5, std::complex> complexMatrix;)
Since the class template tt(Matrix) uses a default data type
a matrix of tt(double)-valued elements can be defined like this:
verb( Matrix<3, 5> doubleMatrix;)
A class template object may be em(declared) using the keyword ti(extern).
hi(class template: declaration)
For example, to em(declare) the matrix tt(complexMatrix) use:
verb( extern Matrix<3, 5, std::complex> complexMatrix;)
hi(reference: to class template)
hi(pointer: to class template)
hi(class template: reference to)
hi(class template: pointer to)
A class template declaration suffices to compile return values or
parameters that are of class template types. Example: the following source
file may be compiled, although the compiler hasn't seen the definition of the
tt(Matrix) class template. Generic classes and (partial) specializations may
all be declared. A function expecting or returning a class template object,
reference, or parameter automatically becomes a function template itself. This
is necessary to allow the compiler to tailor the function to the types of
various actual arguments that may be passed to the function:
verbinclude(-a examples/matrixdecl.cc)
When class templates are em(used) the compiler must first have seen their
implementations. So, template member functions must be known to the compiler
when the template is instantiated. This does not mean that em(all) members of
a template class are instantiated or must have been seen when a class template
object is defined.
hi(class template: member instantiation)
The compiler only instantiates those members that are actually used. This
is illustrated by the following simple class tt(Demo) that has two
constructors and two members. When we use one constructor and call one member
in tt(main) note the sizes of the resulting object file and executable
program. Next the class definition is modified in that the unused constructor
and member are commented out. Again we compile and link the program. Now
observe that these latter sizes are identical to the former sizes. There are
other ways to illustrate that only used members are instantiated. The ti(nm)
program could be used. It shows the symbolic content of object files. Using
tt(nm) we'll reach the same conclusion: em(only template member functions that
are actually used are instantiated). Here is the class template tt(Demo) that
was used for our little experiment. In tt(main) only the first constructor
and the first member function are called and thus only these members were
instantiated:
verbinclude(-a examples/instantiations.cc)
|