Is there any way to use one template definition for multiple function parameter definition... | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

Is there any way to use one template definition for multiple function parameter definition...

Is there any way to use one template definition for multiple function parameter definition... For eg... template <class T> T sum(T a, T b) { return (a+b); } T sub (T c , T d) { return (c-d); } This throws an error that the type T us not defined in "sub" function... So does that mean that one template is only used for a single function? If so, then is there any other way to use T in multiple functions?

18th Oct 2018, 4:14 PM
Rashim Narayan Tiku
Rashim Narayan Tiku - avatar
2 Respostas
+ 2
I am assuming you mean C++... No. The template acts like a modifier to the next definition so only has effect on it. You could put your functions together in a class, which you made a template of. However, what is wrong about putting it twice once on each function. Won't that give you the effect you are after?
18th Oct 2018, 10:09 PM
John Wells
John Wells - avatar
+ 1
thanks John Wells... Actually I was just curious about it cause it is nowhere mentioned in the c++ tutorial here that it only works with one function...
19th Oct 2018, 2:19 AM
Rashim Narayan Tiku
Rashim Narayan Tiku - avatar