Template Question IMPORTANT!!

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

 
T(&arr)[S]


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.
Last edited on
what is being passed to T()?
Nothing. That's just the syntax to pass an array by reference.
type (&formal_parameter_name)[size]
Not every string of the form "identifier(whatever)" implies a function call. For example, int("hello").

if the arr array contains class objects, will T(&arr) call the copy constructor?
No. It's a pass by reference. No function calls are involved.

what is being passed to the ctor : the whole array itself?
It's not possible to pass arrays to functions. Only pointers to arrays may be passed. Still, no function call actually takes place.
Ok, The compiler just blindly replaces T and S with the provided type and value, so it would look like this:

 
int(&arr)[4]


Wouldn't int(&arr)act as a C-style cast and cast the array into an int if a different type of array was given as an argument?
Last edited on
Wouldn't int(&arr)act as a C-style cast
You're still thinking of similar-looking syntaxes as having similar effects. "int(&arr)[4]" just means "this parameter is passed as a reference to an array of size 4 and is named 'arr'". The particular syntax chosen by the language designers is more or less arbitrary. They could have made you do this instead:
1
2
template <typename T, size_t S>
size_t Find(T& value, array_by_reference @ T @ arr @ S)


cast the array into an int if a different type of array was given as an argument?
If you pass an array of non-ints, one of two things can happen:
* If the types of value and the elements of arr are the same, a different instance of the template function will be used, and T will equal whatever other type you used at that particular call site.
* If they are different the compiler will reject the call, because your template definition requires that the elements of arr have the same type as value.
Thank you.
Topic archived. No new replies allowed.