defaulting leading template parameters?

Sean Kelly sean at f4.ca
Wed Nov 1 07:20:29 PST 2006


Bruno Medeiros wrote:
> Sean Kelly wrote:
>> d-bugmail at puremagic.com wrote:
>>>
>>> void testfunc(DUMMY = void, T)(int x, T a) {
>>>     writefln("x = %d, a = %s", x, a);
>>> }
>>
>> I had no idea this was legal--it isn't in C++.  I guess the rationale 
>> is that if the compiler can figure out the remaining parameters by 
>> inspecting function arguments then this is not an error?
> 
> Well, I've never actually learned C++ templates[*], so I my thinking 
> isn't bound to what C++ can or cannot do :P . Anyway, I'm not sure I 
> understand your question. At the time I just checked the docs to see 
> what could be done with IFTI, and it was stated that:
> "Template arguments not implicitly deduced can have default values:
>   void Foo(T, U=T*)(T t) { U p; ... }
> "
> So in both explicit and implicit instantiation you can also have 
> parameters that are deduced from other parameters. (Not the case in C++ 
> then?) The DUMMY case above is just a case where such deduction is 
> constant and not actually dependent on any other parameters.

In C++, template parameter defaults work just like function parameter 
defaults--they can only be used for the final N parameters.  Also, 
defaults can only be used for classes in C++, not functions.  But I 
really like the way D works here instead.


Sean



More information about the Digitalmars-d-bugs mailing list