On Wikipedia, factual accuracy is oftentimes optional
Yup, no doubt about
-
What will happen with your ICE with typedef struct XYZ { ... } XYZ; ? - asked a senior dude during the micro-conference.
Who? What?
For the Wikipedia you are free to choose both the tag-name and the type-name
typedef struct tag_name
{
} type_name;
and it doesn't discourage you from choosing
type_name = tag_name, which is the senior's question: what will happen in this case?
typedef struct myxyz // <---------- collision? or will it work?
{
} myxyz; // <---------- collision? or will it work?
His doubt was solid because, at the open brace, his old C compiler registers myxyz as a structure tag name, and then when myxyz appears a second time, you get a syntax error, clarified as "redeclaration error", so he was curious to understand how our ICE behaves in that case.
Crucial question -
although it is not a problem for the modern c compiler, it's not recommended ... - I replied
-
yes, but what about your ICE in this specific case? - he frowning asked
The screen was forming an expression of disapproval, typically by turning down the corners of the mouth -
well, ... it depends - I glossed, and we got five second of reciprocal awkwardness.
His colleague then asked me why I am using "typedef struct tag type" in the first place, since, he pointed point out , one should *typedef something* when its data is inaccessible (opaque), or when the data is not supposed to be edited by hand, and in both cases they will probably only use the type in one header, in which case you can omit the tag (which is nothing but a true struct-name).
And they don't like to use the tag in "typedef struct tag" because seniors still fear that every name you add to the global namespace increases the chance of a collision.
Now, back to his question, I got a quick flashback on the standard workaround for the above trouble (to avoid collision), which an article on Dr. Dobb's Journal described as the simple trick of making the structure tag name a minor modification of the typedef name, most typically by putting a word e.g. "tc" or "sc" in front.
typedef struct tc_myxyz // <---------- thanks to this trick
{
} myxyz; // <---------- no more collision
We are talking about 1989-1995, there was kind of inertia to adapt to the standard, and this is what programmers do with ancient C compilers where using the same name causes a name collision.
Standard C begins with C89 and modern C compilers separate the structure and typename name spaces, the workaround is no longer necessary, but some-people is still used to use it.
And, sometimes that's not what you get with some tools; for example our ICE ... in performance analysis keeps structure tags, union tags, and typedefs in the same namespace to save precious RAM, so in this case it will report collision-errors.