The compiler cannot generate code for a template until you:
For information on the first two requirements, see "Declaring and Defining Templates."
To instantiate templates automatically, include the template definition file in all the source files that use the template, and just use the template members as you would any other type or function. The compiler automatically generates code for a template instantiation whenever it sees a new one. Listing 4.11 shows how to automatically instantiate the templates in Listing 4.8 and Listing 4.9, class Templ and class Max.
#include <iostreams.h>
#include "templ.cp" // includes templ.h as well
void main(void) {
Templ<long> a = 1, b = 2;
// The compiler instantiates Templ<long> here.
cout << Max(a.Get(), b.Get());
// The compiler instantiates Max<long>() here.
}template class class-name<templ-specs>;
The syntax for a function template instantiation is
template return-type func-name<templ-specs>(arg-specs)
Listing 4.12 shows how to explicitly instantiate the templates in Listing 4.8 and Listing 4.9.
myinst.cp: Explicitly Instantiating Templates:#include "templ.cp" template class Templ<long>; // class instantiation template long Max<long>(long,long); // function instantiation
When you're explicitly instantiating a function, you do not need to include in templ-specs any arguments that the compiler can deduce from arg-specs. For example, in Listing 4.12 you can instantiate Max<long>() like this:
template long Max<>(long, long); // The compiler can tell from the arguments // that you're instantiating Max<long>()
NOTE