+ 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); }
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
0
thanks Raizelđ