Template argument deduction and default args

Manu via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 24 00:00:59 PDT 2014


On 24 July 2014 16:22, Daniel Murphy via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> "Manu via Digitalmars-d" <digitalmars-d at puremagic.com> wrote in message
> news:mailman.243.1406177619.32463.digitalmars-d at puremagic.com...
>
>  The other case I am running in to is when I have 'struct S(T)' or 'class
>> C(T)', where T > can be inferred from the constructor, but it isn't.
>>
>> struct S(T)
>> {
>>   this(T t)
>>   {
>>     m = t;
>>   }
>>
>>   T m;
>> }
>>
>
> Infer this:
>
> struct S(T)
> {
>    static if (is(T == int))
>        this(float x) {}
>    static if (is(T == float))
>        this(int x) {}
> }
>

I imagine that would be a compile error; the static if can't be resolved
without knowing T, and prior to resolution of the static if, no
constructors exist.

Also, the constructor args don't reference T anyway, so I see no reason why
it would ever want to try and deduce T in this situation.
In my example, the constructor implies T, in your example, T implies the
constructor... it doesn't make logical sense the way you present.

Personally, I wouldn't want the compiler to attempt to deduce T in this
case you present, even if it were theoretically possible. It looks like the
programmer intended something very specific in this case. I'd rather have
compile errors when I pass incorrect things to explicit argument types.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20140724/e6b21140/attachment.html>


More information about the Digitalmars-d mailing list