Not sure what is & syntax is supposed to mean here

Write your question here.

1
2
3
4
5
6
7
template <typename Comparable>
const Comparable & findMax(const vector<Comparable> & a )
{int maxIndex=0;
 for(int i =1;i <a.size();i++)
 if(a[maxIndex] < a[i])
  maxIndex=i;
 return a[maxIndex];}


the other code is not really important, i just wanna know what does the & before the findmax function means and how does it work? i know for the other parts, it meanlys pass by reference like the & a, but i don't know what if does here for a function and how it works.
Is the function returning a const reference to a Comparable type? Instead of putting the & directly after the type, there is an extra space.

Edit:

Any reason why the for loop starts at 1? the first value is missed out.

Cheers :+)
Last edited on
i'm just asking about the syntax, like i said the code is not important, i just wanted to know what the & findmax() means, i've need seen a & then a space then a function name like & findmax() before so i don't know what it means.
As I was saying, the function returns a const reference, maybe more conventionally written like this:

const Comparable& findMax(const vector<Comparable> & a ) {

Seen as it is a template, it may have been more conventionally written like this:

1
2
3
4
5
6
7
8
9
template <typename T>
const T& findMax(const vector<T>& a ) {
     int maxIndex=0;
     for(int i =1;i <a.size();i++)
          if(a[maxIndex] < a[i])
     maxIndex=i;

 return a[maxIndex];
}


So if T was an int say, then it returns a const ref to an int type.

Does this help?
not really, cause i don't know what you mean by "So if T was an int say, then it returns a const ref to an int type."

sorry i'm noob :(
The & belongs to the return type. The return type of the function is const Comparable&. The function returns a reference to the maximum Comparable object of the vector.
ok thanks peter87, that helped

so the & belongs to the & and not findmax? the space between the & sign confused me. is there such thing as:

const Comparable &findMax()


?
Last edited on
That would mean exactly the same. As long as there's no ambiguity, then you can be flexible with the white space.

There is no syntactical construction in C++ of the format type & functionName() in which the & would be associated with the function name. The only possible interpretation is that the & is associated with the return type.
Last edited on
The & belongs to findmax from the point of view of the grammar, and if you were crazy enough to declare more than one function on the same line, it would matter:
1
2
3
4
5
6
const Comparable foo, &findmax(), arr[3], &rfoo = foo, *findmaxp();
// foo is a const Comparable
// findmax is a function returning a reference to a const Comparable
// arr is an array of three const Comparables
// rfoo is a reference to const Comparable
// findmaxp() is a function returning a ponter to a const Comparable 


But logically, & is part of the return type, so many C++ programmers put the space after the & to make that clear:
const Comparable& findmax();

To the language, the spacing doesn't matter at all.
Topic archived. No new replies allowed.