C++ Variable Templates
Over the years C++ has undergone advancements to improve its capabilities and coding efficiency. One notable addition in C++14 is the introduction of variable templates. These variable templates allow you to create a group of variables or static data members that can be customized providing flexibility for your code.
Variable Templates
Variable templates are used to create parameterized variables. This feature is enabled in the C++ 14 version of the C++ programming language. It has made creating constants and then using them for different datatypes easy for programmers.
Need of Variable Templates
Variable templates help the developers in defining parameterized constants with ease and simplicity to understand the types and values of constants used in the code. It provides flexibility while dealing with mathematical constants, configuration values, or other compile-time constants that depend on the type or dimensions.
Syntax of Variable Templates
Variable templates are defined using the following syntax:
template <parameters> variable-declaration
where,
- parameter: It is the list of template parameters.
- variable-declaration: Declaration of the variable.
Let's take a look at a simple example of a variable template that defines the constant e (euler):
template <class T>
constexpr T e = T(2.718281828459045); //a variable template
In this code, euler (e) is a variable template that can be instantiated with different types. We'll see how to use it in the below code.
Example of Variable Template
// C++ program to illustrate the use of variable template
#include <iostream>
using namespace std;
// declaring thhe variable template
template <class T> constexpr T e = T(2.718281828459045);
int main()
{
cout << "Integer Type of e: " << e<int> << endl;
cout << "Float Type of e: " << e<float> << endl;
return 0;
}
// C++ program to illustrate the use of variable template
using namespace std;
// declaring thhe variable template
template <class T> constexpr T e = T(2.718281828459045);
int main()
{
cout << "Integer Type of e: " << e<int> << endl;
cout << "Float Type of e: " << e<float> << endl;
return 0;
}
Output
Integer Type of e: 2 Float Type of e: 2.71828
Variable Templates in Class
The variable templates in classes is used to declare static data members template.Also, for static data member templates, you may need to provide definitions outside the class definition.
Example
// C++ program to illustrate the use of static data member
// template
#include <iostream>
using namespace std;
class limits {
public:
// static data member template declaration
template <typename T> static const T min;
};
// definition
template <typename T> const T limits::min = T(10.24);
int main()
{
cout << limits::min<int> << endl;
cout << limits::min<float>;
return 0;
}
// C++ program to illustrate the use of static data member
// template
using namespace std;
class limits {
public:
// static data member template declaration
template <typename T> static const T min;
};
// definition
template <typename T> const T limits::min = T(10.24);
int main()
{
cout << limits::min<int> << endl;
cout << limits::min<float>;
return 0;
}
Output
10 10.24
In this example, we first declare a static data member template min within the limits class. Then, we provide a definition for min outside the class.
Implicit Instantiation of Variable Templates
Just like class and function templates, the types of variable templates can also be implicitly deduced by the compiler in the case where there is no type provided in the template instantanization.
Example of Implicit Instatiation
In this example. we will take a constant pi and will not specify the type of the constant.
// C++ Program to illustrate Variable Templates
#include <iostream>
using namespace std;
// Variable template for pi
template <class T> constexpr T pi = T(3.1415926535897L);
// Function template that calculates the area of a circle
template <class T> T circular_area(T r)
{
return pi<T> * r * r;
}
int main()
{
// Example 1: Using pi with int
int radius_int = 10;
int area_int = circular_area(radius_int);
cout << "Area of circle with radius " << radius_int
<< " is " << area_int << endl;
// Example 2: Using pi with double
double radius_double = 10.0;
double area_double = circular_area(radius_double);
cout << "Area of circle with radius " << radius_double
<< " is " << area_double << endl;
return 0;
}
// C++ Program to illustrate Variable Templates
using namespace std;
// Variable template for pi
template <class T> constexpr T pi = T(3.1415926535897L);
// Function template that calculates the area of a circle
template <class T> T circular_area(T r)
{
return pi<T> * r * r;
}
int main()
{
// Example 1: Using pi with int
int radius_int = 10;
int area_int = circular_area(radius_int);
cout << "Area of circle with radius " << radius_int
<< " is " << area_int << endl;
// Example 2: Using pi with double
double radius_double = 10.0;
double area_double = circular_area(radius_double);
cout << "Area of circle with radius " << radius_double
<< " is " << area_double << endl;
return 0;
}
Output
Area of circle with radius 10 is 300 Area of circle with radius 10 is 314.159
Applications of Variable Templates
Following are some of the major application of the variable templates:
- It is used to define the type dependent constants.
- It provides a way to write the generic code that will work for different data types.
Conclusion
Variable templates in C++14 provide a powerful mechanism to create families of variables or static data members with parameterized types. They enhance code expressiveness and reusability by allowing you to define variables that adapt to different data types. By understanding the syntax and usage of variable templates, one can write more flexible and generic code in C++.