Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters in c++ templates

Tags:

c++

templates

Function template example

template<typename T, int n>
T max(T (&arr)[n])
{
   T maxm = arr[0];
   for(int i = 1; i <n; ++i)
      if (maxm < arr[i])
       maxm = arr[i];

   return maxm;
}

Is arr also a type parameter like T ?

like image 580
username1234 Avatar asked Jul 20 '26 12:07

username1234


1 Answers

arr is a name of a function parameter. It's not a type parameter. Its type is a reference to an array of element type T and length n.

like image 118
julx Avatar answered Jul 23 '26 04:07

julx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!