Variable template - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# Variable template

From cppreference.com
< cpp‎ | language

A variable template defines a family of variables or static data members.

### [edit] Syntax

`template` `<` parameter-list `>` declaration |  |

### [edit] Explanation

declaration defines or declares a [[cpp/language/class_template|class (including struct and union), a function or member function, a static data member at namespace scope, a variable or static data member at class scope, (since C++14) or a alias template (since C++11) It may also define a template specialization. This page focuses on variable templates.

A variable template may be introduced by a template declaration at namespace scope, where declaration declares a variable.

    template<class T>
    constexpr T pi = T(3.1415926535897932385);  // variable template

    template<class T>
    T circular_area(T r) // function template
    {
        return pi<T> * r * r; // pi<T> is a variable template instantiation
    }

When used at class scope, variable template declares a static data member template.

    struct matrix_constants
    {
        template<class T>
        using pauli = hermitian_matrix<T, 2>; // alias template

        template<class T>
        constexpr pauli<T> sigma1 = { { 0, 1 }, { 1, 0 } }; // static data member template

        template<class T>
        constexpr pauli<T> sigma2 = { { 0, -1i }, { 1i, 0 } };

        template<class T>
        constexpr pauli<T> sigma3 = { { 1, 0 }, { -1, 0 } };
    };

A definition of a static data member template may be provided outside the class definition. A template declaration of a static data member at namespace scope may also be a definition of a non-template data member of a class template:

    struct limits {
        template<typename T>
        static const T min; // declaration of a static data member template
    };
    template<typename T>
    const T limits::min = { }; // definition of a static data member template

    template<class T>
    class X {
       static T s; // declaration of a non-template static data member of a class template
    };
    template<class T>
    T X<T>::s = 0; // definition of a non-template data member of a class template

Unless a variable template was explicitly specialized or explicitly instantiated, it is implicitly instantiated when a specialization of the variable template is used.

Default template argument of a variable template is implicitly instantiated when the variable template is used in the context that requires the value of the argument.

### [edit] Notes

Until variable templates were introduced in C++14, parametrized variables were typically implemented as either static data members of class templates or as constexpr function templates returning the desired values.

Variable templates cannot be used as template template arguments.

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/language/variable_template&oldid=72402"