I want to know about templates in c++. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I want to know about templates in c++.

Why we are using T, N, U....So on and what is the meaning of them?? Syntax: template <class T > template <class U> template <class N> Can someone tell???

18th Sep 2020, 5:45 PM
Rupali Haldiya
Rupali Haldiya - avatar
4 Answers
+ 5
Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type. The format for declaring function templates with type parameters is: template <class identifier> function_declaration; template <typename identifier> function_declaration; The only difference between both prototypes is the use of either the keyword class or the keyword typename. Its use is indistinct, since both expressions have exactly the same meaning and behave exactly the same way.
18th Sep 2020, 5:51 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
For example, to create a template function that returns the greater one of two objects we could use: template <class myType> myType GetMax (myType a, myType b) { return (a>b?a:b); } if you want to learn templates you can visit this site https://www.google.com/amp/s/www.geeksforgeeks.org/templates-cpp/amp/
18th Sep 2020, 5:53 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
rupali welcome
18th Sep 2020, 6:04 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Thanku maam.
18th Sep 2020, 6:00 PM
Rupali Haldiya
Rupali Haldiya - avatar