Re: template overload resolution
On Jul 31, 5:41 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
hurcan solter wrote:
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?
AFAIK each argument is matched with corresponding parameter. in that
case shouldnt T1* and T2* match better than T for (int*)? I assume T1
and T2 are disjoint aren't they? or must they be different types?
A template with fewer arguments is considered more specialised than one
with more arguments, I guess. The more arguments a template has, the
more generic it is.
I think that the original poster was expecting the partial
ordering of function templates, described in =A714.5.6.2 to enter
into play. I'll admit that I have great difficulty in
understanding this myself, but roughly speaking, I think the
idea is that the one function template is more specialized than
another if all of its specializations could also be valid
specializations of the other. Thus, for example, in:
template< typename T > void f( T ) ;
template< typename T > void f( T* ) ;
the second is more specialized, because all possible
specializations of it could also be specializations of the
first, where as the reverse is not true.
If this is really an accurate description, his functions
obviously aren't ordered, since it is trivial to find
specializations of one which can't possibly be a specialization
of the other, e.g.: foo( int, int ) can only specialize the
first, and foo( int*, double* ) can only specialize the second.
*IF* I understand it correctly (and as I say, this is so abtuse
that I'm far from sure), the partial ordering is present over
the function templates themselves, independantly of any
particular attempts to specialize.
Those are just guesses, of course. I don't have time to look through
the Standard for confirmation.
I think at a higher level, you've given the best possible
answer. If even better than average programmers have to study
the standard to figure out what is going on, then your code is
too complex, and you should avoid such constructs.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34