Site icon Learn C++

Learn About Extern Templates In Modern C++

Learn About Extern Templates In Modern C++

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++.

What is a template in C++?

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.

[crayon-6763d37f7db5b620603150/]

The parameters of a template can be,

For example, we can declare a template as below.

[crayon-6763d37f7db62694796729/]

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:

[crayon-6763d37f7db63710837299/]

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:

[crayon-6763d37f7db65886011322/]

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++?

[crayon-6763d37f7db66742387654/]

What are the rules to use extern template In C++?

Here are the rules for using extern templates:

[crayon-6763d37f7db68534887838/]

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

Exit mobile version