new DIP40: Template parameter deduction for constructors

deadalnix deadalnix at gmail.com
Tue May 14 03:23:39 PDT 2013


On Tuesday, 14 May 2013 at 09:56:14 UTC, Timon Gehr wrote:
> DMD's strategy is roughly: use the first eponymous declaration 
> that can be found without analysing the template body for IFTI, 
> then use the first eponymous declaration in the analyzed 
> template body to resolve the eponymous declaration after 
> instantiation.
>
> ---
> import std.stdio;
> template fun(T){
> 	int fun(double arg){ return 1; }
> 	int fun(T arg){ return 0; }
> }
> void main(){ writeln(fun(2)); } // error
>
> ---
> import std.stdio;
> template fun(T){
> 	int fun(T arg){ return 0; }
> 	int fun(double arg){ return 1; }
> }
> void main(){ writeln(fun(2)); } // ok
>
> ---
>
> This has funny implications, as the compiler may decide to 
> resolve to a different declaration than was used for IFTI 
> later, without doing any kind of overload resolution within the 
> template body.
>
> ---
> template fun(T){
> 	int fun(T arg){ return 0; }
> 	static if(true) int fun(double arg){ return 1; }
> }
> pragma(msg, fun(2)); // 0
> ---
> template fun(T){
> 	static if(true) int fun(double arg){ return 1; }
> 	int fun(T arg){ return 0; }
> }
> pragma(msg, fun(2)); // 1
> ---
>
> In the second case, instantiation is performed with the second 
> function, so T is resolved to 'int', but in the end, the 
> 'double' overload is called with an implicit conversion.

That creative. But completely b0rken IMO


More information about the Digitalmars-d mailing list