When we should use templates?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

When we should use templates??

2nd Dec 2017, 8:21 PM
Aman Raj
Aman Raj - avatar
4 Answers
+ 4
whenever we want to use same function or variable or class for different datatype and want to decide datatype at compilation time then we like to uss templates rather than declaring or defining them for all the datatypes
19th Dec 2017, 2:54 PM
himani
+ 7
Whenever we need something to be decide at compilation but we do not know what it is
2nd Dec 2017, 9:30 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 6
@~swim~ Your scenarios are definitely good and not redundant. If I may expand on your various examples, I would add... If you can isolate a common pattern with a function where the logic is the same regardless of the typed data used for parameter inputs and /or the typed data used for the return type, templates might be a good fit. Essentially, templates are a great way to abstract your logic from any specific typed data. As long as that typed data abides to the same interfaces and operations, you can swap specific types with generic types.
3rd Dec 2017, 8:01 AM
David Carroll
David Carroll - avatar
+ 2
thanks everyone for solving my query!!
11th Feb 2018, 2:07 AM
Aman Raj
Aman Raj - avatar