+1 vote
in C Plus Plus by
Variable templates C++ 14 features

1 Answer

0 votes
by
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:

template<>

constexpr const char* pi<const char*> = "pi";

Related questions

+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
+1 vote
+1 vote
asked Jan 4, 2020 in C Plus Plus by AdilsonLima
...