why this output is 1 ,why not 2 ? template <class T> void f(T &i){ cout<<1; } template<> void f(const int &i){ cout<<2; } int main (){ int i=8; f(i); } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why this output is 1 ,why not 2 ? template <class T> void f(T &i){ cout<<1; } template<> void f(const int &i){ cout<<2; } int main (){ int i=8; f(i); }

25th Aug 2016, 1:18 PM
Lekhraj Singh
Lekhraj Singh - avatar
2 Answers
+ 2
Because the variable i is not constant It will write 1 is the variable is not constant And 2 is it is constant Because the function is overloaded The first form take as a parameter ( T &i) which means if the variable is of the type T and not constant this one will be executed And the second one take as parameter ( const T &i) which means if the variable type is T and it is constant this one will executed
25th Aug 2016, 11:26 PM
Raizel TheNoblesse
Raizel TheNoblesse - avatar
0
thanks Raizel👍
26th Aug 2016, 4:10 AM
Lekhraj Singh
Lekhraj Singh - avatar