Template vs function preference ( function having same name as template with same number of parameters)
So I stumbled upon this code (code attached), Here if a call f() with int it gives 2 as output, but if i change the parameters to take a reference (even if I change both template and function to receive reference) rather than value, it gives 1 as output. So basically my question is if template and a function are identical then which one would be called?(Normally the function is called but if i change the parameters to reference the template is called, so can someone clear this out and explain how this all works?). https://code.sololearn.com/c8qaz0o0AcdP/?ref=app Edit : ~ swim ~ So b/w T i and int i complier will choose int i, is that right?. One more thing, is this compiler dependent or applies to every c++ complier?
8/25/2019 10:06:47 AMbufftowel
2 AnswersNew Answer
Basit Between T& i and const int& the compiler will choose T& as call matches better with T&. const int& is a different type alltogether Between T& and int& compiler will choose int& as call matches more closely to int parameter Compiler always tries to choose most specialized form.
Basit It's Standard, name lookup, argument deduction and selection rules apply. And yes compiler will choose int i between T i and int i if 'i' is of type int.