Template method and overloading

Bill Baxter dnewsgroup at billbaxter.com
Tue May 29 16:18:41 PDT 2007


Jarrett Billingsley wrote:
> "Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
> news:f3i3uc$2vkn$1 at digitalmars.com...
>> "gerleim" <elf_qt at _deletethisifyouarenotaspammer_yahoo.com> wrote in 
>> message news:f3hvvt$2q18$1 at digitalmars.com...
>>> Oh, I see, thanks. Didn't know that template parameter lists have to 
>>> differ.
>>> (Is that documented?)
>>>
>> It's because templated functions are really just syntactic sugar.  So:
>>
>> void foo(T)(int x){}
>> void foo(T)(int x, float y){}
>>
>> Is really seen by the compiler as:
>>
>> template foo(T) { void foo(int x){} }
>> template foo(T) { void foo(int x, float y){} }
>>
>> Now it looks obvious that there should be an error, because it's the 
>> templates that are at top-level, not the functions.
> 
> But after posting that, I now don't know how the compiler thinks this is any 
> easier to deal with:
> 
> template foo(T) { void foo(int x){} }
> template foo(T, dummy = void) { void foo(int x, float y){} }
> 
> How does it find the foo(int x, float y)?  Maybe it's buried in weird 
> template rules I don't entirely understand.. 

I think that's the SFINAE thing ("Somethinerother Failure Is Not An 
Error").  It tries one template, and that fails to work, so it just goes 
on to try the next until it finds one that works.  Or maybe it finds all 
the ones that work then picks the "best match" of the lot.  (if you have 
a foo(int T) and a foo(float T) for instance).  Either way in the above 
example only one template instantiation works so it's not hard to decide.

--bb


More information about the Digitalmars-d-learn mailing list