templates

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 19 13:58:22 PDT 2010


On Mon, 19 Apr 2010 16:52:40 -0400, Philippe Sigaud  
<philippe.sigaud at gmail.com> wrote:

> On Mon, Apr 19, 2010 at 21:20, Steven Schveighoffer  
> <schveiguy at yahoo.com>wrote:
>
>>
>> Here is a quick-and-dirty solution, if you don't mind using
>>> classes/interfaces.
>>>
>> (snip)
>
> I'm not used to using interfaces in this way. What become the stored T
> values when you cast the classes into IW to construct your array? I  
> suppose
> they're lost?

Not sure what you mean...

>
> Does that mean that if we have a bunch of T (all different types),you  
> map a
> call to makeW on it, get back a bunch of WByVal!T (all different types),
> cast them to IW and... what? erase the types?

What I had in mind was:

struct S
{
   int foo(int x) { return x * 2; }
}

struct S2
{
   int foo(int x) { return x + 2; }
}

void main()
{
    S s1;
    S2 s2;

    IW[] ws;

    ws ~= makeW(s1);
    ws ~= makeW(s2);

    assert(ws[0].foo(3) == 6);
    assert(ws[1].foo(3) == 5);
}

does this help?

-Steve


More information about the Digitalmars-d-learn mailing list