Hi guys, I had this question thrust into my head, why constexpr template functions would work? As shown in the code below, it is a constexpr template function which returns the size of a given array of unknown type.
1 2 3 4 5 6
|
#include <iostream>
using namespace std;
template<typename T, const int a> constexpr const int f(T (&x)[a])
{
return a;
} Put the code you need help with here.
|
As far as I am concerned, constexpr functions are ones that can be evaluated at compile time, but template functions, no matter how I look at them, could not be the kind of functions that can be evaluated at compile time. And my reason is that the compiler only generates the code for a template function when it first meets one that is being used. For example, when I first defined the template function above, the compiler would not possibly know what type of T and what value of a I was going feed to this function, so it could not generate the function of the template, but then if in the main function, I had code like
1 2 3
|
int main()
int arr[3] = {1, 2, 3};
cout << f(arr];
|
then, the compiler would generate a corresponding version of the template function for this specific type of array.
And this mechanism seems much like what I learned about dynamic binding used with inherited classes, and I know that for dynamic binding, virtual functions are chosen to be used at runtime, so I conjecture that the template functions doing their instantiation at runtime as well, which would contradict with constexpr function being evaluated at compile time.