Re: template overload resolution
On Aug 1, 7:18 am, Craig Scott <audiofana...@gmail.com> wrote:
On Aug 1, 2:19 am, hurcan solter <hsol...@gmail.com> wrote:
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?
As far as the compiler is concerned, both can be matched equally well.
In this case. If the choice were between:
template< typename T >
void foo( T, T ) ;
and
template< typename T >
void foo( T*, T* ) ;
however, there is a partial ordering of template
"specializations" which comes into play: basically, given two
function templates, if any time type deduction works for the
first, it works for the second, but there are cases where type
deduction can work for the second, but not for the first, then
the first is said to be "more specialized" than the second.
(Note that the use of "specialized" here has nothing to do with
template specialization.)
No conversions are involved in either case, so there is no
reason for the compiler to favour one over the other.
There isn't in this particular case, but there might be in other
cases. There is a partial ordering among function templates,
and that ordering does play a role in overload resolution.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientie objet/
Beratung in objektorientierter Datenverarbeitung
9 place Simard, 78210 St.-Cyr-l'Icole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]