Recursive data structure using template won't compile

Philippe Sigaud philippe.sigaud at gmail.com
Thu Nov 8 09:57:01 PST 2012


Rob, your original code:

// d-linked list with templated payload
struct d_list( T )
{
    struct node
    {
       T payload;
       node* pred;
       node* succ;
    }
    node* head;
    node* tail;
}

compiles just fine for me (Linux 32bits, DMD 2.060).

Even with some exercising, the template doesn't fail:

void main()
{
    auto list = d_list!(int)();
}


On Thu, Nov 8, 2012 at 6:49 PM, Rob T <rob at ucora.com> wrote:

> On Thursday, 8 November 2012 at 15:19:56 UTC, Dmitry Olshansky wrote:
>
>>
>> Naturally C++ doesn't have nested struct definitions.
>>
>>
> I don't have a nested struct, the struct definition defines the type only,
> and the nodes are allocated as pointers from a new operation when the list
> is composed. The node struct has a member var that may or may not be a
> struct type, it does not matter, and C++ will allow a member data type to
> be a struct just fine.
>
>
>  I also want to template the recursive structure itself to specify the
>>> value type, ie struct R(T){ T value; d_list!R Rlist; }, but I left that
>>> out to keep the example more simple.
>>>
>>>
>>  I've been stuck on this problem all day, so any help is appreciated!
>>>
>>
>> See if it helps.
>>
>
> I tried moving the node struct outside the d_list struct, but no luck, and
> I think it should not matter anyway since I can get it to work in that way
> without a template.
>
> The solution so far is to do it the C++ way, using pointers, which sucks.
>
> I think in this case the D template should work just fine, and the problem
> is either a bug or a design flaw with how templates are evaluated.
>
> I'll wait a bit longer for more comments before filing a bug report.
>
> --rt
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20121108/ca395c8a/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list