One of the great features of modern C++ is templates. 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 explain Variable Template in C++ examples that can be used by a professional C++ Code Editor and compiler that supports C++14, C++17, and over.
First of all, let’s try to explain 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 let the user may 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 entity 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 C++?
A variable template is used to define a family of variables or static data members. If a static data member instantiated from a static data member template, it is called as an instantiated static data member. Note that, a variable template can be introduced by a template declaration at namespace scope, where variable-declaration declares a variable.
Syntax of a Variable Template;
1 2 3 |
template < parameter-list > variable_declaration |
Where is a simple variable template example in C++?
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 C++?
Now, let’s use this in a template. We can define a force template (force = mass x gravity) as 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 variable template example in C++?
Here is a example about class template and template instantiation of this class template,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> // Variable Template Example template<class T> constexpr T gravity = T(9.80665); // variable template // Let's define a Force template, F = mass x gravity template<class T> T force(T mass) // function template { return mass*gravity<T>; // gravity<T> is a variable template instantiation } int main() { double f = force(82.5); // using function template std::cout << "Force:" << f << " Nt" << std::endl; system("pause"); return 0; } |
What is wrong when using a variable template in C++?
When using a variable template be sure about declared variable type, otherwise you may have wrong results or missing data. Here is the example below, it shows why you cannot use int
type as an input to declare f5, f6 integer variables as below.
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 30 31 32 33 34 35 36 |
#include <iostream> // Variable Template Example template<class T> constexpr T gravity = T(9.80665); // variable template // Let's define a Force template, F = mass x gravity template<class T> T force(T mass) // function template { return mass*gravity<T>; // gravity<T> is a variable template instantiation } int main() { double f1 = force(1000.0); // using function template (correct) std::cout << "f1:" << f1 << " Nt" << std::endl; int f2 = force(1000.0); // using function template (correct) std::cout << "f2:" << f2 << " Nt" << std::endl; int f3 = force((double)1000); // using function template (correct) std::cout << "f3:" << f3 << " Nt" << std::endl; int f4 = force<double>(1000); // using function template (correct) std::cout << "f4:" << f4 << " Nt" << std::endl; int f5 = force(1000); // using function template (wrong) std::cout << "f5:" << f5 << " Nt" << std::endl; int f6 = (double) force(1000); // using function template (wrong) std::cout << "f6:" << f6 << " Nt" << std::endl; system("pause"); return 0; } |
and the output will be as follows,
1 2 3 4 5 6 7 8 9 |
f1:9806.65 Nt f2:9806 Nt f3:9806 Nt f4:9806 Nt f5:9000 Nt f6:9000 Nt Press any key to continue . . . |
as you see f5 and f6 are the wrong values here, these options doesn’t help to get correct data from this variable template. Because our template type looks the type of the input variable that we use, so it should be float or double to use its precision side.
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
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition