C++C++11C++14C++17Learn C++SyntaxTemplates

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.

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.

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:

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:

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

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:
Learn About Extern Templates In Modern C++ the C++ Builder Logo

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?