alias this with template class?

Timon Gehr timon.gehr at gmx.ch
Sun Jun 3 12:26:48 PDT 2012


On 06/03/2012 08:43 PM, Namespace wrote:
> I think this should work:
>
> [code]
> import std.stdio;
>
> class Foo(T) {
> public:
>      T Num;
>
>      @property
>      Foo!(U) ConvertTo(U)() inout {
>          return cast(Foo!(U)) this;
>      }
>
>      alias ConvertTo this;
> }
>
> void Call(const Foo!(float) FFoo) {
>
> }
>
> void main() {
>      Foo!(ubyte) UbFoo = new Foo!(ubyte)();
>
>      Call(UbFoo);
>
>      readln();
> }
> [/code]
>
> Why it doesn't?

Making this generally work as you expect would be too hard.
The compiler would have to reverse-engineer the 'ConvertTo' template
which is an undecidable problem in general. Some clever heuristics
might suffice for most practical instances though, but those are
very open-ended and therefore it is hard to incorporate them into the
language definition. If you think it should be done you could file a
suggestion (enhancement request) into the bug tracker.


More information about the Digitalmars-d-learn mailing list