The template feature in C++ is one of the great capabilities of modern C++. A template is a simple and very powerful statement in C++ that defines the operations of a class or function. In this article, we will show how extern templates can be used in a modern C++ app based on recent C++ standards.
First of all, let’s remind ourselves of what templates are in C++.
Table of Contents
What is a template in C++?
A template is a very powerful statement in C++ that simply defines the operations of a class, a function, an alias, or a variable. It lets the user apply the same template on different types to increase code reuse. Templates are like macros in C++, except the compiler checks the types used before the template is expanded. In the compilation mechanism of a template in C++, the source code contains only a template for a function or class, but when it is compiled, the same template can be used on multiple data types.
Here is the syntax of a template.
1 2 3 |
template < parameters > declaration |
The parameters of a template can be,
- type template parameter,
- non-type template parameter,
- template template parameter (a template used as a parameter).
For example, we can declare a template as below.
1 2 3 4 5 |
template<class T> class my_temp { }; |
What is extern Template In C++?
An extern template allows you to declare a template without instantiating it in the translation unit. In other words, you can use the extern template
to force the compiler to not instantiate a template when you know that it will be instantiated somewhere else. Basically, an extern template is used to reduce compile time and object file size.
To illustrate, the following both creates and instantiates a template:
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 |
extern template class MyClass<int>; |
You can then reference the template, but the compiler does not generate code for it in that translation unit.
Is there a full example about extern template in C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> template <class T> class mytemp { T x; }; template class mytemp<int>; extern template class mytemp<float>; int main() { mytemp<int> my_class; mytemp<float> my_class2; } |
What are the rules to use extern template In C++?
Here are the rules for using extern templates:
- A template instantiation must either follow an explicit template declaration in that translation unit or be in another translation unit. This is similar to the usual use of extern: the entity referenced must be defined somewhere.
- An explicit template definition must occur only once in a unit.
- An explicit template definition must follow an explicit template declaration if both are present in a translation unit.
- An explicit template declaration can only apply to names of objects, functions, and explicit template instantiations. It may not refer to a static function but may apply to a static member function.
- The extern specifier may not be used in the declaration of class members or parameters.
- An explicit template declaration only suppresses code generation if that template has not been instantiated with the same specialization. For instance:
1 2 3 4 5 6 |
template class MyClass<int>; ... extern template class MyClass<int>; // not allowed extern template class MyClass<float>; // OK |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here