What are the advantages of using templates in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What are the advantages of using templates in C++?

12th Nov 2018, 5:13 AM
Shreyas Bobde
Shreyas Bobde - avatar
3 Answers
+ 7
By using templates we can implement different data types . Instead of using any particular data type we can use a template and then later convert it into any data type we want.
12th Nov 2018, 6:39 AM
Qurat
Qurat - avatar
+ 3
Not worried much about overloading, like template <typename T> T m(T x, T y){ return (x>y)?x:y; } int main() { cout<<m<int>(1,3)<<endl; cout<<m<char>('x','X'); return 0; } I'm sure there are some more
12th Nov 2018, 6:05 AM
Taste
Taste - avatar
+ 2
Template are inline and such as your template code will not use function call but it will be "expanded" at compile time with right types by compiler (its not a real advantage in all case but its useful to know)
12th Nov 2018, 7:17 AM
KrOW
KrOW - avatar