Why code failed to compile for foo2?

apz28 home at home.com
Mon Dec 13 23:17:56 UTC 2021


On Saturday, 11 December 2021 at 23:44:59 UTC, Adam Ruppe wrote:
> On Saturday, 11 December 2021 at 23:17:17 UTC, Stanislav Blinov 
> wrote:
>> ? No. If it was unsatisfied constraint, the error would've 
>> shown that.
>
> And if you try to instantiate it, you'll see it is an 
> unsatisfied constraint anyway. There's two layers of failure 
> here.
>
> Using Unqual there is pretty iffy, i wouldn't bother with it at 
> all, but if you do anything, instead qualify it const.
>
> But either way, then the constraint still fails since int isn't 
> unsigned.
>
> I'd really recommend simplifying this a lot.

1. This is why there is a diverse logic for language rule vs 
template rule.
The overload functions enjoy all benefits of implicit conversion 
rule but none for template

2. This is why template implementation create more function 
bloats more than it needs to be as compilable example below

     import std.traits : isUnsigned, Unqual;
     void foo2(T, alias UT1 = Unqual!T)(T x)
     // You can create alias UT1 but not able to use it in 
function parameter
     if(isUnsigned!T)
     {
         alias UT2 = Unqual!T;
         pragma(msg, T.stringof);
         pragma(msg, UT1.stringof);
         pragma(msg, UT2.stringof);
     }

     void main()
     {
         import std.math;

         int s = 1;
         foo2(cast(uint)abs(s));
         foo2(cast(const(uint))abs(s));
     }

Output as below
uint
uint
uint
const(uint)
uint
uint


More information about the Digitalmars-d-learn mailing list