问题
I am wondering how, in the following piece of code, the compiler deduces the arrsize template argument from the T (&arr)[arrsize] function argument. For example, when I pass a 4-element array to it, without mentioning the number 4 in my call to the function, it correctly determines the arrsize argument to be 4. However, if I pass the array normally (not as a reference to array), that is, if I change T (&arr)[arrsize] to T arr[arrsize], it requires me to explicitly provide the arrsize argument in the template argument list.
template <class T, int arrsize> void bubblesort(T (&arr)[arrsize], int order=1)
{
if (order==0) return;
bool ascending = (order>0);
int i,j;
for (i=arrsize; i>0; i--)
for (j=0; j<i-1; j++)
if (ascending?(arr[j]>arr[j+1]):(arr[j]<arr[j+1])) swap(arr[j],arr[j+1]);
}
So my question is:
How does the compiler figure out the value of the
arrsizeargument automatically when I pass to the function a reference to an array? (What is the mechanism?)Why can the compiler not do the same if I pass the array normally? (by normally I mean without using the reference symbol)
回答1:
- It can deduce the size because the size is known at compile-time within the calling context. If you have
int a[4], and you writebubblesort(a), then the compiler uses the fact that the type ofaisint[4]to deducearrsizeas 4. If you try to dobubblesort(p)whenphas typeint*, deduction will fail and a compile error will result. - If you write
T arr[arrsize]as the parameter instead ofT (&arr)[arrsize], then the compiler will automatically rewrite the declaration asT* arr. Sincearrsizeno longer occurs in the signature, it can't be deduced.
回答2:
T arr[arrsize] as a formal argument decays to just T* arr, where the arrsize is ignored completely (as indeed is the array nature of that argument).
来源:https://stackoverflow.com/questions/37952603/how-does-the-compiler-deduce-array-size-when-defined-as-a-template-parameter