Doubt about template used

what is the use of declaring template<class forwarditerator,class T> how does the generic function identify this?

@S19LPPP0015 hey basically template ap ko datatype generalise krne ke kam ate hai,he simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types. For example, a software company may need sort() for different data types. Rather than writing and maintaining the multiple codes, we can write one sort() and pass data type as a parameter.

C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The second keyword can always be replaced by keyword ‘class’.

How templates work?
Templates are expanded at compiler time. This is like macros. The difference is, compiler does type checking before template expansion. The idea is simple, source code contains only function/class, but compiled code may contain multiple copies of same function/class.
templates-cpp
Function Templates We write a generic function that can be used for different data types. Examples of function templates are sort(), max(), min(), printArray().
#include
using namespace std;

// One function works for all data types. This would work
// even for user defined types if operator ‘>’ is overloaded
template
T myMax(T x, T y)
{
return (x > y)? x: y;
}

int main()
{
cout << myMax(3, 7) << endl; // Call myMax for int
cout << myMax(3.0, 7.0) << endl; // call myMax for double
cout << myMax(‘g’, ‘e’) << endl; // call myMax for char

return 0;
}
Output:
7
7
g

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.