Implicit conversion to templatized type

Paul Backus snarwin at gmail.com
Fri Nov 6 15:36:46 UTC 2020


On Friday, 6 November 2020 at 15:01:21 UTC, Andrey Zherikov wrote:
>
> But how can I achieve the same result if S1 is a template 
> "struct S1(T) {}" and S2 should be convertible to S1!T with any 
> T?
>
> Also why neither "opCast"
>     struct S2 { S1 opCast(T)() const if(is(T == S1)) { return 
> S1(); } }
> nor suitable ctor
>     struct S1 { this(const S2 s){} }
> are used for implicit conversion?

This is impossible by design. User-defined implicit conversions 
are one of the most error-prone features of C++, and have been 
deliberately excluded from D, with the exception of `alias this`.


More information about the Digitalmars-d-learn mailing list