Open In App

C++ Variable Templates

Last Updated : 13 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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


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


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.


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:

  1. It is used to define the type dependent constants.
  2. 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++.


Next Article
Practice Tags :

Similar Reads