Template Generation Semantics

From RAD Studio
Jump to: navigation, search

Go Up to Compiler Template Switches Index

The BCC32 compilers generate the following methods for template instances:

  • Those methods which were actually used
  • Virtual methods of an instance
  • All methods of explicitly instantiated classes

The advantage of this behavior is that it results in significantly smaller object files, libraries and executable files, depending on how heavily you use templates.

Optionally, you can use the '-J' switch to generate all methods (see Template Compiler Switches and C++ Compiler).

You can also force all of the out-of-line methods of a template instance to be generated by using the explicit template instantiation syntax defined in the ISO/ANSI C++ Standard. The syntax is:

template class classname<template parameter>;

The following STL example directs the compiler to generate all out-of-line methods for the "list<char>" class, regardless of whether they are referenced by the user's code:

template class list<char>

You can also explicitly instantiate a single method, or a single static data member of a template class, which means that the method is generated to the .OBJ even though it is not used:

template void classname <template parameter>:: methodname ();

See Also