I came to know about C++14 comes with variable template.
template<typename T>
constexpr T pi = T(3.1415926535897932385);
My question is - When we use variable templates over normal variable? Please Give me some example.
I came to know about C++14 comes with variable template.
template<typename T>
constexpr T pi = T(3.1415926535897932385);
My question is - When we use variable templates over normal variable? Please Give me some example.
One of the properties of templates is support of explicit (and partial) specializations. I'd guess that this would apply to variable templates as well, allowing you to provide individual initializers for different specializations, as in
template<typename T>
constexpr T pi = T(3.1415926535897932385);
template<>
constexpr float pi = 3.1415;
template<>
constexpr MyFractionType pi = MyFractionType(22, 7); // close enough for most purposes
template<>
constexpr int pi = 3; // :)
As it's been mentioned in the comments, it is already possible to "templatize and specialize variables" by wrapping them into a class (as static members). Variable templates would allow one to do that without resorting to a class-wrapper-based workaround. In that sense, template variables solve the same issues as template typedefs do.