+ 6
When we should use templates??
6 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
+ 7
Whenever we need something to be decide at compilation but we do not know what it is
+ 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.
+ 2
thanks everyone for solving my query!!