I was reading my book "Professional C++ Third Edition" and they gave the following example to explain function templates. What it is is basically a function that finds a value in a container (like std::find() ). In the function template that they use, they call another function called Calculate() to calculate the result:
1 2 3 4 5 6
|
template <typename T, size_t S>
size_t Find(T& value, T(&arr)[S]
{
return Calculate(value, arr, S)
}
|
Ok. So lets say you called the function like this:
1 2
|
int x = 3, intArr[] = {1,2,3,4};
size_t result = Find(x, intArr);
|
The part that confuses me is the second parameter of the Find() function
So the compiler will automatically deduce the size of S to be the size of the input, but what is being passed to
T()
? More importantly, if the
arr
array contains class objects, will
T(&arr)
call the copy constructor? What if the the type
T
is just a simple type like
int
or
double
?
Also, if the
T()
call calls the copy ctor, then what is being passed to the ctor : the whole array itself?
Please respond quickly as I have spend a lot of time trying to find the answer from the strange syntax provided.