Jan 4 in C++

Q: Variable templates C++ 14 features

1 Answer

Jan 4
In prior versions of C++, only functions, classes or type aliases could be templated. C++14 now allows the creation of variables that are templated. An example given in the proposal is a variable pi that can be read to get the value of pi for various types (e.g., 3 when read as an integral type; the closest value possible with float, double or long double precision when read as float, double or long double, respectively; etc.).

The usual rules of templates apply to such declarations and definitions, including specialization.

template<typename T>

constexpr T pi = T(3.141592653589793238462643383);

// Usual specialization rules apply:


constexpr const char* pi<const char*> = "pi";
Click here to read more about C++
Click here to read more about Insurance

Related questions

Jan 4 in C++
Jan 4 in C++
Jun 13, 2019 in C++