opCast / operator overloading with additional template arguments

Paul Backus snarwin at gmail.com
Mon Jan 11 04:40:15 UTC 2021


On Monday, 11 January 2021 at 03:40:41 UTC, Paul wrote:
> On Monday, 11 January 2021 at 00:48:49 UTC, Steven 
> Schveighoffer wrote:
>> I would think though, that this should work:
>>
>> T opCast(T : Vec!(vecsize, S), S)()
>
> Oh wouw, this seems to work perfectly! Awesome thanks ^^
>
> Any Idea why
>> T opCast(T, S)() const if (is(T : Vec!(grootte, S))) {
> yields the error
>> template instance opCast!(Vec!(2, double)) does not match 
>> template declaration opCast(T, S)()
> while your suggestion does not? It seems to me it should match 
> equally well.

The compiler does not look at template constraints until after it 
figures out what all the template arguments are. So, in your 
version, it sees

     T opCast(T, S) // ...
     opCast!(Vec!(2, double))

...and is able to deduce that `T = Vec!(2, double)`, but doesn't 
have any way to figure out what `S` is, so it gives up.


More information about the Digitalmars-d-learn mailing list