[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


Instantiating a Template

The compiler cannot generate code for a template until you:

For information on the first two requirements, see "Declaring and Defining Templates."

Specifying the data type(s) and other arguments for a template is called instantiating the template. CodeWarrior C++ gives you two ways to instantiate a template. You can let the compiler instantiate it automatically when you first use it, or you can explicitly create all the instantiations you'll need.


Automatic instantiation

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.

myprog.cp: A Source File that Uses Templates:
#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.
}

If you use automatic instantiation, the compiler may take longer to compile your program because it has to determine on its own which instantiations you'll need. Also, the object code for the template instantiations will be scattered throughout your program.


Explicit instantiation

To instantiate templates explicitly, include the template definition file in a source file, and write a template instantiation statement for every instantiation. The syntax for a class template instantiation is


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>()

If you use explicit instantiation, the compiler compiles your program more quickly. Because the instantiations can be in one file with no other code, you can even choose to put them in a separate library.


NOTE

Explicit instantiation is not in the ARM but is part of the ANSI/ISO C++ standard.


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 17, 2000