template <class T>
void func(T*& a)
{
}
int main()
{
int a[5];
func(a); // <-- Error here: no matching function for call to ‘func(int [5])’
return 0;
}
why int[] is not implicitly converted to int* so that it can match template functions?
>Solution :
There are 2 things that are important here:
-
Since the parameter
ais a reference type, the passed argument is not passed by value. In other words, conversion like array to pointer decay will not happen for the passed argument. -
Since the parameter is a reference to a pointer to a
T, the passed argument must be of a pointer type.
Now, lets apply this to your example:
By writing func(a), you’re passing the argument a which is of array type int [5] by reference so that it will not decay to a pointer to int(int*). In other words, the parameter expects that you pass an argument of pointer type by reference but you’re passing an array type by reference(so that there is no decay to pointer type) and hence the error.