template and what is created during compilation
if i have a template function:
template<class T, class S>
void foo(T t, S s){..//do something//}
and then, inside the main i do this:
string str = "something";
char* ch = "somthingelse开发者_JS百科";
double num = 1.5;
foo(ch, num);
foo(num, ch);
foo(str, num);
..
my question is in the compilation what code will be written at the executable? is it will be:
foo<char*, double>(..);
foo<double, char*>(..);
foo<string, double>(..);
or the compile will know at the second call to foo to change the place of the classes. or in the 3rd one, in implicit way to use char* to create a string class?
Usually it will instantiate all three. They will not seek default-cast-workarounds to save binary image space.
il will not implicitly use
foo<string, double>(...)
for
foo(str, num)
but you can explicitly ask to use it, i.e. by calling
foo(string(str), num)
I think the following quote from the Standard clarifies this:
$14.9.1/6- "Implicit conversions (Clause 4) will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter type contains no template-parameters that participate in template argument deduction. [ Note: template parameters do not participate in template argument deduction if they are explicitly specified."
Since in this case the parameter types of the function template participate in function template argument deduction, no implicit conversion e.g. of string to char * take place.
精彩评论