The template is one of the great features of modern C++. They are a simple and very powerful statement in C++ that defines the operations of a class or function. The C++14 standard and above allows the creation of variables that are templated. In this article, we will explain variable templates in C++ with examples that can be used in a professional IDE and compiler that supports C++14, C++17, and over.
First of all, let’s remind ourselves what a C++ template is.
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 and lets the user apply the same template on different types in those template operations. Templates are like macros in C++, considered in compilation except compiler checks types used before the template is expanded in its terms. 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.
Templates are powerful entities that can be parameterized by one or more parameters. These parameters can be type template parameters, non-type template parameters, and template template parameters (parameters which are themselves templates).
What is a variable template in modern C++?
A variable template is used to define a family of variables or static data members. If a static data member is instantiated from a static data member template, it is called an instantiated static data member. A variable template can be introduced by a template declaration at namespace scope, where a variable declaration declares a variable. The C++14 standard and above allows the creation of variables that are templated. In C++11, only functions, classes, or type aliases could be templated.
Syntax of a variable template;
1 2 3 |
template < parameter-list > variable_declaration |
The usual rules of templates apply to such declarations and definitions, including specialization.
Where is a simple variable template example in modern C++?
Variable templates are used to instantiate the variables separately for the type. For example, we can assign 9.80665 to the g variable in T form ( i.e. T(9.80665) ) as a float or double or long double type and that would be g or g or g or g in template usage.
Here is a simple variable template example that is used to define standard gravity value.
1 2 3 |
template<class T> constexpr T gravity = T(9.80665); |
Is there a variable template instantiation example in modern C++?
Now, let’s use this in a template. We can define a force template (force = mass x gravity) as shown below:
1 2 3 4 5 6 |
template<class T> T force(T mass) // function template { return mass*gravity<T>; // gravity<T> is a variable template instantiation } |
here gravity<T>
is a variable template instantiation.
Is there a full example of variable templates in modern C++?
Here is an example variable template instantiation to different types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> template<typename T> T gravity = T(9.80665); // template<class T> constexpr T gravity = T(9.80665); template<class T> T force(T mass) // function template { return mass*gravity<T>; // gravity<T> is a variable template instantiation } int main() { auto g1 = gravity<int>; auto g2 = gravity<float>; auto g3 = gravity<double>; gravity<int> = 10; auto g4 = gravity<int>; std::cout << g1 << std::endl; std::cout << g2 << std::endl; std::cout << g3 << std::endl; std::cout << g4 << std::endl; system("pause"); return 0; } |
and the output will be:
1 2 3 4 5 6 |
9 9.80665 9.80665 10 |
Here, as you see g
is 9 for the int
type but after our new definition it is considered as 10.
You can use variable templates in other templates too. Here is another full example about variable template instantiation,
For more information about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3651.pdf
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