tags:

views:

60

answers:

3

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";
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?

+3  A: 

Usually it will instantiate all three. They will not seek default-cast-workarounds to save binary image space.

Pavel Radzivilovsky
+2  A: 

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) 
sergiom
+1  A: 

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.

Chubsdad
@Chubsdad can i ask you where did you find this?
or.nomore
C++ draft standard (N3126)
Chubsdad
@Chubsdad : N3126 is not[yet] a Standard. I would recommend you to quote from the C++03 Standard(C++0x is not yet a Standard mind you) instead of quoting from N3126 etc etc...
Prasoon Saurav