RAD Studio |  |

Template Generation Semantics

The C++ compiler generates 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 ‘-Ja’ switch to generate all methods.

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:

Copy 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:

Copy Code

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

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!