Define parameterized constant with Variable Templates

Question | Jul 15, 2019 | nextptr 

It is possible to define templated variables since C++14, this feature is called Variable Templates. Prior to C++14 only functions and classes (or structs) could be templated. The most important use of Variable Templates is in defining parametrized constants (constants for different built-in or user-defined types). The proposal document Variable Templates provides plenty of reasonings and examples on parametrized constants.

Here, we take an example of a numeric constant, Euler's number e, which needs to be defined for various numeric types (e.g., int, float, double) to handle different precisions. The Euler's number e can be defined with Variable Template as:

template <typename T>
constexpr T e = T(2.7182818284590452353602874713527);

Now, we can have Euler's number constants for different numeric types e.g., e<int>, e<double>. Consider the following code to see the difference between e<int> and e<double>:

std::cout << std::setprecision(8) << e<int> << std::endl;
std::cout << std::setprecision(8) << e<double> << std::endl;

What will be the output of the above? Select the correct choice below (check Explanations for details on the correct answer):