Thread overview
How can I induce implicit type convesion with alias this on calling template function?
Oct 15, 2018
Sobaya
Oct 15, 2018
Alex
Oct 15, 2018
Sobaya
October 15, 2018
void func(T : int)(T value) if (is(T == int)) {
}

struct S {
    int x;
    alias x this;
}

void main() {
    func(S()); // error
}

In above code, 'func' can accept only int as its argument type, so when 'S', which can be implicitly convertible into int, is passed on 'func', I expect S.x is passed, but this function call is failed.

Is there any way to solve it with keeping 'func' template function?

October 15, 2018
On Monday, 15 October 2018 at 04:51:39 UTC, Sobaya wrote:
> void func(T : int)(T value) if (is(T == int)) {
> }
>
> struct S {
>     int x;
>     alias x this;
> }
>
> void main() {
>     func(S()); // error
> }
>
> In above code, 'func' can accept only int as its argument type, so when 'S', which can be implicitly convertible into int, is passed on 'func', I expect S.x is passed, but this function call is failed.
>
> Is there any way to solve it with keeping 'func' template function?

Removing constraint, but retaining specialization should be enough, no?
Then, func is still a template, requiring the argument to be convertible to an int. When S is passed, then, it is checked, if it convertible, and because of the alias it is.
Still, passed value has the type of S. however, func can handle value directly, as it were an int.

October 15, 2018
On Monday, 15 October 2018 at 06:16:34 UTC, Alex wrote:
> On Monday, 15 October 2018 at 04:51:39 UTC, Sobaya wrote:
>> [...]
>
> Removing constraint, but retaining specialization should be enough, no?
> Then, func is still a template, requiring the argument to be convertible to an int. When S is passed, then, it is checked, if it convertible, and because of the alias it is.
> Still, passed value has the type of S. however, func can handle value directly, as it were an int.

Thank you!