Defining Variable Templates

Learn to generate variables with different types based on the template parameters.

Variable templates were introduced in C++14 and allow us to define variables that are templates either at namespace scope, in which case they represent a family of global variables, or at class scope, in which case they represent static data members.

Variable templates

A variable template is declared at a namespace scope as shown in the following code snippet. This is a typical example that we can find in the literature, but we can use it to elaborate on the benefits of variable templates:

Get hands-on with 1200+ tech skills courses.