Re: template overload resolution
{ Note: multi-posted to [comp.lang.c++]. -mod }
On 8/5/07 6:32 PM, in article MPG.21200fd4f45a673e98997d@news.sunsite.dk,
"Jerry Coffin" <jcoffin@taeus.com> wrote:
In article <1185891029.530703.158380@b79g2000hse.googlegroups.com>,
hsolter@gmail.com says...
given the code snippet;
template<typename T>
void foo(T,T){}
template<typename T1,typename T2>
void foo(T1*,T2*){}
int main( ) {
foo((int*)0,(int*)0);
}
Could anyone please explain me why the second template is not more
viable for overload resolution?
$14.5.5.2/3:
For each type template parameter, synthesize a unique type
and substitute that for each occurrence of that parameter
in the function parameter list, or for a template
conversion function, in the return type.
So, in the first case, T becomes "int *". In the second case, T1 and T2
both become "int". The result is two candidate functions, each of which
takes two parameters, both of type "int *".
No, the paragraph cited from the Standard is describing how two, overloaded
function templates may be "partially-ordered" in relation to one another.
And since a partial ordering describes a relationship strictly between the
function templates themselves - a partial ordering never varies. Therefore
the types of arguments passed in the function call requiring overload
resolution - is irrelevant to the process of determining whether a partial
ordering exists between a pair of function templates. In other words, it is
possible to determine whether any two, overloaded function templates are
partially ordered in relation to each other - simply by performing the
comparison described by the Standard.
Furthermore, since the paragraph instructs us to "synthesize" a type - we
are free to come up with any type that we like. And because we are looking
for a type (or set of types) that will be a match for one foo() but not the
other, choosing "int *" as our synthesized type - is not a very good choice.
foo( int *, int *) is a match for either foo function template - so int * is
not the kind of synthesized type that we should be looking for.
Now, if we synthesize an "int" type instead - we are in business. Because a
foo with two int parameters like so:
foo( int, int )
is a match for:
foo( T, T )
but not a match for:
foo( T1 *, T2 * )
- because an "int" (of course) is not any kind of pointer type.
So at this point it appears that the foo( T1*, T2*) is the more specialized
function template - unless we can synthesize type arguments that would match
in the opposite manner. Can we do so? As it turns out, yes, we can. We can
synthesize a foo with two different pointer types as parameters - like so:
foo( int *, long *)
is a match for;
foo( T1 *, T2 *)
but not match a match for:
foo( T, T )
Because, of course, int * and long * are not the same type.
Therefore, since we were able to come up with one set of type arguments that
matched only one of the foo template functions but not the other ( and found
another set of types with the opposite matching behavior), we conclude that
neither foo function template is more specialized than the other. So with no
partial ordering to select one foo over the the other - the call is
ambiguous.
It is also worth point out - as James did earlier - that if foo( T, T ) were
replaced with foo( T1, T2 ) - then a partial ordering would exist between
the two foo() function templates.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]