Nontype template parameter

Hi guys, why doesn't this code compile?

1
2
3
template <class T, size_t N> 
       void array_init(T parm[N])
{ /*...*/ }


but this one does:

1
2
3
template <class T, size_t N> 
       void array_init(T (&parm)[N])
{ /*...*/ }


Thanks in advance.
What error are you getting?

The following compiles on mingw and msvc
(note that I include iostream to make sure that size_t is properly declared)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

 template <class T, size_t N> 
       void array_init(T parm[N])
{ /*...*/ } 


template <class T, size_t N> 
       void array_init(T (&parm)[N])
{ /*...*/ }


int main()
{
    
  int my_array[5];  //array
  
  int (&ref_array)[5] = my_array; //reference to an array
  
  array_init ( my_array);
  array_init(ref_array);


}
Oh, my mistake.
The code I posted does compile - BUT both calls at line 20 and 21 are using the second template void array_init(T (&parm)[N]).

If you have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

template <class T, size_t N> 
       void array_init(T parm[N])
{ /*...*/ } 


int main()
{  
  int my_array[5];  //array  
  
  array_init ( my_array);
}

then it does not work, because in C/C++ when you think you are passing an array, you are actually passing a pointer.

So this actually works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

template <class T> 
       void array_init(T*)
{ /*...*/ } 


int main()
{  
  int my_array[5];  //array  
  
  array_init ( my_array);
}


Thanks for your replies

then it does not work, because in C/C++ when you think you are passing an array, you are actually passing a pointer.


How does this work then?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

void array_init2(int parm[])
{ /*...*/ }

int main()
{
  int my_array[5];  //array

  array_init2( my_array);
}


Couldn't be that the template has some restriction or something?

by the way, the error I get is:

 
error: no matching function for call to ‘array_init(int [5])’
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)
Last edited on

templates are somewhat more strict


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>
using namespace 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>
using namespace 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;
}

Last edited on
func<int>(arr );

works in that case.

You can try like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

template <typename T, int SIZE>
void array_int(T*& parm)
{
	parm = new T[SIZE];
	strcpy(parm, "hello");
}

int main()
{
	char* ptr;
	array_int<char, 10>(ptr);
	std::cout << ptr << std::endl;
	delete [] ptr;
	return 0;
}
you can not pass a array to a function, no matter it is a NORMAL function or a template

function, just pass a pointer to the function, then tell it the size of the array. In this place, the

size is just the Nontype template parameter.
Last edited on
Topic archived. No new replies allowed.