Function templates do implicit conversions for their arguments

Maxim Fomin maxim at maxim-fomin.ru
Tue Jul 2 12:39:54 PDT 2013


On Tuesday, 2 July 2013 at 16:59:50 UTC, TommiT wrote:
> ...

Slightly changed original code

struct Wrap(T)
{
     T t;
}

struct S
{
     int id;

     Wrap!S getWrapped()
     {
         return Wrap!S(this);
     }

     alias getWrapped this;
}

T tearOpen(T)(Wrap!T wrappedT)
{
     return wrappedT.t;
}

void main()
{
     S ted = S(123);
     S r = tearOpen(ted);
     assert(r == ted);
}

Since both S(123) and tearOpen(ted) has same type and return same 
value comparison succeeds. tearOpen(ted) should take Wrap!T but 
argument has different type. Argument has alias this, so it is 
analyzed and since it is aliased to Wrap!S, passing succeeds. 
This is how alias this is designed to work. What have you 
surprised here?


More information about the Digitalmars-d mailing list