RAD Studio
|
C++Builder 2009 includes the use of extern templates, which allow you to define templates that are not instantiated in a translation unit. Using extern templates thus reduces both compilation time and the size of the compiled module. This feature is part of the new C++0x standard.
An extern template allows you to declare a template without instantiating it in the translation unit.
To illustrate, the following both creates and instantiates a template:
template <class T> class MyClass { //various code } template class MyClass<int>; ... MyClass<int> myClass;
The line template class MyClass<int> is an explicit template definition and causes the template to be instantiated explicitly in its code unit, resulting in generating code for the template in that unit. Similarly, the line MyClass<int> myClass; implicitly instantiates the template, also resulting in code generation in the unit. If either of these lines of code are in your unit, the template is instantiated there.
However, suppose you want to have a library in which all instantiations of this template occur, and you want to refer to these instantiations in an executable. To make an explicit template declaration that does not instantiate the template in your code unit, use the following:
extern template class MyClass<int>;
You can then reference the template, but the compiler does not generate code for it in that translation unit.
Here are the rules for using extern templates:
template class MyClass<int>; ... extern template class MyClass<int>; // not allowed extern template class MyClass<float>; // OK
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|