This was motivated by this article (page 5)
template<class T>
T const &f(T const &a, T const &b){
return (a > b ? a : b);
}
template int const &f<int>(int const &, int const &);
int main(){
int x = 0, y = 0;
short s = 0;
f(x, y); // OK
f(x, s); // Is this call well-formed?
}
Is the call 'f(x, s)'
well-formed? I assumed that since the function template 'f'
is explicitly instantiated, standard conversions would be applied and hence 'short s'
would be converted to 'int'
to match the call to the explicit specialization 'f<int>'
. But it appears that this is ill-formed?
Which part of the Standard talks about the applicable rules in this context?
The call
f(x, s)
is syntactically well-formed, but the compiler will not be able to deduce the template parameterT
from it because is could aint
or ashort
(because of the first and second arguments). Instantiating the template does not help, that only indicates the compiler to compile that specialization and add it to the generated object file.If you want the call to cast
s
to aint
automatically, usef<int>(x, s)
.