Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why int[] cannot be converted to T*&?

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?

like image 660
N.Y.C Avatar asked Oct 23 '25 00:10

N.Y.C


1 Answers

There is an implicit conversion from int[5] to int*, but an int[5] is not an int*. Thus there is no int* for the int*& parameter of func<int> to reference.

This situation is no different from trying to pass an int to a function expecting a float&. Just because an int can be implicitly converted to a float doesn't mean an int is a float. The exact same thing applies to int[5] and int*. Just because one can be implicitly converted to the other doesn't mean that they are the same thing.

like image 172
Miles Budnek Avatar answered Oct 24 '25 13:10

Miles Budnek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!