Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are 2 things that are important here:

  1. Since the parameter a is 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.

  2. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading