I have a structure defined in a header, serving as a node in a doubly-linked list. I'm in the habit of defining a struct like this, so that I don't have to prefix every reference to it with 'struct':
STYLE #1:typedef struct {
DLISTNODE* Next;
DLISTNODE* Prev;
void* Data;
} DLISTNODE;
But the compiler doesn't accept that on this particular struct, having the unusual property of being self-referential. Ok, no problem, I can do this instead:
STYLE #2:typedef struct STRDLISTNODE DLISTNODE;
struct STRDLISTNODE {
DLISTNODE* Next;
DLISTNODE* Prev;
void* Data;
};
#define ZINLINE __attribute__((always_inline)) static __inline__
ZINLINE DLISTNODE* DListNext(DLISTNODE* n) { return n->Next; }; // Gets next node.
Now the compiler is happy, and the code runs correctly. But the IDE can't figure it out, and marks every reference to DLISTNODE members in red, like 'return n->
Next'. Leaving my code littered with false errors, making it hard to pick out real errors, and disabling contextual suggestions when writing code using this structure.
Looking up some example code for doubly-linked lists, I found another style, which I'd never seen before:
STYLE #3:typedef struct DLISTNODE {
struct DLISTNODE * Next;
struct DLISTNODE * Prev;
void* Data;
} DLISTNODE;
Now the compiler is happy, and so is the IDE - but only with this particular structure. I have many other (non-self-referencing) structs defined in style #1, which the IDE previously had no issue with, and now it's failing on all of them! It's as if upon seeing the new style once, it's unable to handle any other style. I tried changing one struct definition to the new doubly-named style, and it no longer had issue with that struct.
Leaving me wondering whether I should:
1) Revert the self-referencing structure to style #2, and let the IDE complain about just that one.
2) Convert all my struct definitions to style #3 (this will take a while in 20K lines of code).
Before considering #2, I need to fully understand this new doubly-named style. It seems that it creates overlapping names in the namespace. One being a struct, one being a typedef, and both with the same name. That's normally something I'd avoid, even if allowed, as it introduces ambiguity. Yet I found someone saying it's the "common idiom", and if so, I'd consider that a valid reason to convert:
http://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitionsBut is that really true? Or are there downsides to this? Even if it works on this compiler/IDE, if I were to migrate this code to another C compiler not based on GCC (like Visual C), or perhaps C++, would it still work?