that code works as I say because in C/C++ when you pass an array you are passing a pointer, and the array_init2 is defined as taking an int array - but it will actually be getting a pointer (although inside the function you treat it exactly as an array)
because it is a pointer being passed, the following is also equivalant:
1 2 3 4 5 6 7 8 9
void array_init2(int* parm)
{ /*...*/ }
int main()
{
int my_array[5]; //array
array_init2( my_array);
}
C/C++ does not pass arrays by copy - it is one of those C/C++ things.
That is why you are getting the error: no matching function for call to ‘array_init(int [5])’ error - templates are somwehat more strict (you could possibly use a typedef as a workaround)
Are you saying that this conversion that occurs from the name of an array
to a pointer in a regular function is an error with templates? Because it looks like it.
I reduced my code to the working minimum and this is what I got:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
void func(int arr[])
{
cout << "yuhuu" << endl;
}
int main()
{
int arr[10];
func(arr); // fine prints "yuhuu" on screen
return 0;
}
now with only one extra line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
template <class T> // only line added
void func(int arr[])
{
cout << "yuhuu" << endl;
}
int main()
{
int arr[10];
func(arr); // error: no matching function for call to ‘func(int [10])
return 0;
}