How can I induce implicit type convesion with alias this on calling template function?

Alex sascha.orlov at gmail.com
Mon Oct 15 06:16:34 UTC 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.



More information about the Digitalmars-d-learn mailing list