Given;
template<class T>
constexpr T pi = T(3.1415926535897932385); // when T is double
// and
constexpr double pi = 3.1415926535897932385;
There is no runtime difference, they are both compile time constants. Templates are a compile time thing - as such, when comparing like with like (i.e. constexpr double pi
vs. constexpr T pi
) it would end up being the same - this is expected.
What does make a difference from the OP code is how it is used.
template<class T>
T circular_area_t(T r) // function template
{
return pi<T> * r * r; // pi<T> is a variable template instantiation
}
// and
constexpr double circular_area_1(double r)
{
return pi<double> * r * r;
}
double circular_area_2(double r)
{
return pi<double> * r * r;
}
Given the constexpr
function circular_area_1
and the template function circular_area_t
, both of these result in compile time calculations, hence literals in the binary, of the result. The non-constexpr
function circular_area_2
is compiled as a normal function and is executed at runtime to determine the result. This makes a difference at runtime. See here for a code listing.