Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
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?
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?
+ 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...