Default template parameters and IFTI

Jonathan M Davis jmdavisProg at gmx.com
Sat Aug 13 23:43:46 PDT 2011


On Sunday, August 14, 2011 06:37:27 Lars T. Kyllingstad wrote:
> Today, I was very surprised to discover that the following compiles and
> works:
> 
>     void foo(int i = 123, T)(T x)
>     {
>         import std.stdio;
>         writeln(i, T.stringof, x);
>     }
> 
>     void main()
>     {
>         foo(456.0);  // Prints "123double456"
>     }
> 
> In other words, the compiler allows you to specify a default value for a
> function template parameter which is *not at the end of the parameter
> list* as long as the remaining parameters are automatically deduced.
> 
> Now, my question is:  Is this by design?  If so, I think it would be
> awesome, and I can put it to good use in the new std.path.  I do,
> however, not want to base my code on a compiler bug.

As I understand it, it's by design. I've used it elsewhere. I don't think that 
it _always_ works, since it needs to be unambiguous (e.g. I could get it to 
work for std.utf.toUTFz), but it definitely works at least some of the time. I 
believe that there's stuff in Phobos which does it already, but I'd have to go 
digging.

- Jonathan M Davis


More information about the Digitalmars-d mailing list