Why code failed to compile for foo2?

Stanislav Blinov stanislav.blinov at gmail.com
Sat Dec 11 23:17:17 UTC 2021


On Saturday, 11 December 2021 at 22:59:52 UTC, Adam Ruppe wrote:
> On Saturday, 11 December 2021 at 22:50:45 UTC, apz28 wrote:
>> void foo2(T)(Unqual!T x) if(isUnsigned!T) {}
>
> This means it treats foo2 as if it doesn't exist unless T is 
> unsigned...
>
>> onlineapp.d(15): Error: template `onlineapp.foo2` cannot 
>> deduce function from argument types `!()(int)`
>> onlineapp.d(7):        Candidate is: `foo2(T)(Unqual!T x)`
>> */
>
> And this is telling you it had a signed value - int - which 
> means it doesn't match.
>
> For the other ones, the functions still exist, so it does 
> whatever conversion it needs. Whereas with the template that 
> constraint means the compiler acts like it doesn't exist at all 
> and thus doesn't even attempt an automatic conversion.

? No. If it was unsatisfied constraint, the error would've shown 
that. What's going on here is that the argument being passed is 
an int, when the parameter is an Unqual!T. So, 
https://dlang.org/spec/template.html#ifti would fail because 
there is no way of figuring out a T from an Unqual!T. Hence the 
message: cannot deduce function.


More information about the Digitalmars-d-learn mailing list