-1

This example is taken from: https://learn.microsoft.com/en-us/cpp/cpp/templates-cpp?view=msvc-160

I already tested the code can somebody tell me what it does at C20?

Why is const T& lhs used instead of const lhs used?

My answer is that it is the only thing that works. Maybe somebody can explain the inner workings?

Why is const used at all?

template <typename T> //C10
T minimum(const T& lhs, const T& rhs) //C20
{
    return lhs < rhs ? lhs : rhs; //C30
}

C10= defines T as a template class=template while class is an old way to say template?

C20= what is the ampersand doing here? Why do I have to use it?

C30= conditional operator works like:

if(lhs < rhs)
{return lhs;}
else
{return rhs;}

https://learn.microsoft.com/en-us/cpp/cpp/conditional-operator-q?view=msvc-160

Does this template output any type you throw at it? I tested int, float and string. It works as expected with int and float while it does take strings it does not work as expected. Looks like it could be a sorting algorithm for ordering the display of filenames if somebody beefs it up. It only compares the first char of a string.

Jan Girke
  • 117
  • 2
  • 9

3 Answers3

1

The const keyword prevents from changing the state of the variable, consider the following code

int a = 5;
int b = 6;
int i = minimum<int>(a, b);

when this code is executed:it is equivalent to

 int minimum(const int& lhs, const int& rhs)
 {
   return lhs < rhs ? lhs : rhs;
 }

If you don't set an const you are not warned by the compiler that an constant object is being changed and you can return a new value of the variable,like

int minimum(int& lhs,  int& rhs)
  {
   lhs = 43; // you can change it and may return a changed value
   return lhs < rhs ? lhs : rhs;  
 }

But if it was a const It would prevent you from changing the state.

EDIT: Any programmer would want to preserve the state of its object when they are trying to return the same object,so const is used to preserve the state.

Abhilekh Gautam
  • 648
  • 1
  • 11
  • 27
1

Considering:

template <typename T> //C10
T minimum(const T& lhs, const T& rhs) //C20
{
    return lhs < rhs ? lhs : rhs; //C30
}

Trying to explain one could say:

  • The C10 commented line defines that the function is generic, and accepts T as a template-argument (which is auto-resolved by compiler).
  • C20: The & sign in type-signature means passing by reference (ensuring no copy happens), and one could modify the variable passed to method directly (without need to return) if it was not const.
  • C30: should expand to:
    if(lhs < rhs) {
       return lhs;
    } else {
       return rhs;
    }
    
Top-Master
  • 7,611
  • 5
  • 39
  • 71
1

Why is const T& lhs used instead of const lhs used?

The ampersand indicates that lhs is a reference. You can think of it as a pointer, but with some restrictions to make it safer.

Why is const used at all?

The code would work without const, but it is a good practice to qualify function parameters as const whenever possible - especially non-primitive types. Search for "const correctness" for more information.

C10= defines T as a template class=template while class is an old way to say template?

Basically, yes. See here for more information.

C20= what is the ampersand doing here? Why do I have to use it?

You don't have to use it, but using it may increase performance. Without the ampersand, lhs and rhs are passed by value, whereas with the ampersand these are passed by reference. The latter avoids having to make a copy of a potentially large object.

Does this template output any type you throw at it?

No, it checks how the objects are used. This function will work for any type for which the < operator is defined.

Yun
  • 3,056
  • 6
  • 9
  • 28