-1

In this example:

template <typename T>
inline T const& Max (T const& a, T const& b)  { 
    return a < b ? b : a; 
}

Specifically this part:

...const& Max (T const& a, T const& b)...

I don't understand what the & is being used for. I can make the same function without the & and it would still work. What is the point in getting the address here?

David G
  • 94,763
  • 41
  • 167
  • 253

3 Answers3

3

I can make the same function without the & and it would still work.

No, you can't. Remove the ampersands and you have a very different function. This new function makes copies of the arguments passed to it and returns a copy. With the ampersands, the function does not make copies. The returned value is an alias of the larger input.

Here's another example: Take away the const qualifiers to both the arguments and the return value. With this change the function can be used on the left hand side of an assignment. There's no way you can do this without the ampersands.

#include <iostream>

template <typename T>
inline T & Max (T & a, T & b)  {   
   return a < b ? b : a;  
}

int main () {
  int a = 1;
  int b = 2;

  Max(a,b) = 42; 

  std::cout << "a=" << a << " b=" << b << "\n";
}
David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • Okay, now I understand what the `const` and ampersands is for. Can you tell me why anyone would do what you did, which was `Max(a, b) = 42;`? – David G Aug 02 '12 at 13:28
  • Also, I though `const` was supposed to go at the end of the parameter list as shown here: `int num() const {}`... Why is it at the beginning in my example? – David G Aug 02 '12 at 13:42
  • @David: Regarding your first comment, *Can you tell me why anyone would do what you did, which was Max(a, b) = 42;?*, I simply used your example to illustrate the concept of a function that returns an lvalue. This example isn't particularly useful, but there are plenty of places where such a concept is useful. – David Hammen Aug 02 '12 at 14:58
  • Regarding your second comment, *Why is it at the beginning in my example?*, you are confusing the `const` qualifier on a member function with the return value. A free function such as your `Max` cannot be const-qualified. Whether a member function is (or isn't) const-qualified and whether the return value from a function is (or isn't) const-qualified are two very distinct concepts. – David Hammen Aug 02 '12 at 15:01
  • Re *what does inline do?* It started out in C as a directive to the compiler to embed the body of the function directly in the code at every point where the function is called. This didn't work all that well; programmers tend to be lousy at guessing what is / is not a good candidate for inlining. So it became a hint rather than a directive. (Aside: the compiler is also free to inline functions that aren't qualified with `inline`.) Continued ... – David Hammen Aug 02 '12 at 18:37
  • What `inline` really does is allow one to define a function in a header that is #included in multiple files. Without the `inline` qualifier, the resulting multiple definitions would violate the one definition rule. With the `inline`, those multiple definitions are OK so long as they truly are the same definition. – David Hammen Aug 02 '12 at 18:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14815/discussion-between-david-and-david-hammen) – David G Aug 02 '12 at 18:45
0

The & symbol in this case is not getting the address, but is a creating a reference variable. Think of a reference as a pointer that is always dereferenced. This isn't what it actually is, but its the simplest way to explain it.

JKor
  • 3,822
  • 3
  • 28
  • 36
0

The thought process behind it is basically, "I want to pass by reference (for whatever reason), and while I'm at it I'd like to make sure that it doesn't change along the way. I'll modify the parameter with const.".

The fact that it's a reference means you're not copying the value into your new scope (function). Instead, you're referring to the original version from the scope that called your function. The reason for const is simply to guarantee that the value does not change.

Read the "pass by const reference" section at http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

A more thorough explanation of many details surrounding the usage of const: http://en.wikipedia.org/wiki/Const-correctness

Jacob
  • 21
  • 3