digitalmars.D - How to declare a parameterized recursive data structure?
- artemav (18/18) Aug 15 2014 I'm getting an error when trying to compile (DMD64 D Compiler
- Justin Whear (3/22) Aug 15 2014 You want `node!(T)* next`. Structs are POD types, not reference types,
- bearophile (8/12) Aug 15 2014 Try:
- H. S. Teoh via Digitalmars-d (15/35) Aug 15 2014 This has nothing to do with the struct being parametrized or not.
- artemav (2/21) Aug 15 2014
- artemav (4/29) Aug 15 2014 :) Wow, thanks for all replies. I realized too late that should
- Philippe Sigaud via Digitalmars-d (13/16) Aug 15 2014 Can I add something? ;)
- Gary Willoughby (5/24) Aug 16 2014 Funnily enough i've been toying with linked lists using the same
- safety0ff (7/11) Aug 16 2014 Why did you put the data between the two pointers?
- Gary Willoughby (2/16) Aug 17 2014 Good idea, I didn't think about that.
I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?
Aug 15 2014
On Fri, 15 Aug 2014 18:37:51 +0000, artemav wrote:I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?You want `node!(T)* next`. Structs are POD types, not reference types, so the node struct would have a infinite regression.
Aug 15 2014
artemav:struct node(T) { T data; node!T next; };Try: struct node(T) { T data; node!T* next; } Bye, bearophile
Aug 15 2014
On Fri, Aug 15, 2014 at 06:37:51PM +0000, artemav via Digitalmars-d wrote:I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?This has nothing to do with the struct being parametrized or not. Structs are value types, so defining a struct in terms of itself would make it infinitely large. That's why it's invalid. I'm guessing what you *really* want is to keep a pointer to another struct of the same type, in which case what you're looking for is: struct node(T) { T data; node* next; // Note: it's not necessary to specify // !T here, if you want, you can write // it as: node!T* next; } T -- Having a smoking section in a restaurant is like having a peeing section in a swimming pool. -- Edward Burr
Aug 15 2014
Sorry, jumped the gun on that. On Friday, 15 August 2014 at 18:37:52 UTC, artemav wrote:I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?
Aug 15 2014
:) Wow, thanks for all replies. I realized too late that should write "that's it". Hmm, It's also a good sign that D community is active. On Friday, 15 August 2014 at 19:01:57 UTC, artemav wrote:Sorry, jumped the gun on that. On Friday, 15 August 2014 at 18:37:52 UTC, artemav wrote:I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?
Aug 15 2014
On Fri, Aug 15, 2014 at 9:13 PM, artemav via Digitalmars-d <digitalmars-d puremagic.com> wrote::) Wow, thanks for all replies. I realized too late that should write "that's it". Hmm, It's also a good sign that D community is active.Can I add something? ;) You can also use a class, as they are reference types: class node(T) { T data; node next; } Or use a dynamic array if you want to define a tree: struct node(T) { T data; node[] children; }
Aug 15 2014
On Friday, 15 August 2014 at 18:37:52 UTC, artemav wrote:I'm getting an error when trying to compile (DMD64 D Compiler v2.066) this: struct node(T) { T data; node!T next; }; void main() { node!int n; } output: test.d(3): Error: struct test.node!int.node cannot have field next with same struct type test.d(9): Error: template instance test.node!int error instantiating ... It is so simple code that the error "... cannot have field next with same struct type" looks funny and sad at the same time. Is it a bug or are there some constraints why it cannot be compiled?Funnily enough i've been toying with linked lists using the same kind of nodes here: https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d Might be of use to you?
Aug 16 2014
On Saturday, 16 August 2014 at 17:55:28 UTC, Gary Willoughby wrote:Funnily enough i've been toying with linked lists using the same kind of nodes here: https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d Might be of use to you?Why did you put the data between the two pointers? I would put the pointers side-by-side in memory: - The two pointers will be in the same cache line no matter which type T is used. - It should reduce the amount of padding in the Node struct.
Aug 16 2014
On Saturday, 16 August 2014 at 22:55:37 UTC, safety0ff wrote:On Saturday, 16 August 2014 at 17:55:28 UTC, Gary Willoughby wrote:Good idea, I didn't think about that.Funnily enough i've been toying with linked lists using the same kind of nodes here: https://github.com/nomad-software/etcetera/blob/master/source/etcetera/collection/linkedlist.d Might be of use to you?Why did you put the data between the two pointers? I would put the pointers side-by-side in memory: - The two pointers will be in the same cache line no matter which type T is used. - It should reduce the amount of padding in the Node struct.
Aug 17 2014