In C, if you declare a struct like:
struct point {
int x, y;
};
you also have to use struct when referring to point, e.g.:
struct point p; // declare p as struct point
because all struct, union, and enum names are in a distinct "tags" namespace. Yes, I know you can use a typedef to import a struct's tag name into the current scope:
typedef struct point point;
point p; // don't need "struct" now
Clearly, a tags namespace isn't needed since C++ effectively auto-typedefs struct (and class) declarations.
My question is: does anybody know what Ritchie's rationale for using a separate tags namespace was? Why weren't struct names just put directly into the current scope so a typedef would be unnecessary?
His The Development of the C Language mentions structures, but nothing about the tags namespace.
Note that I’m really looking for a definitive answer, not speculation.
The only minor benefit is that you can have a struct and variable with the same name, e.g.:
struct stat stat;
To me, however, that's outweighed by ordinarily having to use struct all over the place if you don't use typedef.
For self-referential structures, you don't need the struct prefix either since in C++ you could do:
struct link; // forward declare link
struct link {
void *data;
link *next;
};
That works just fine.
Note that I'm not asking about why a lot of Unix struct members have prefixes, e.g., sin_family, sin_port, sin_addr. Aside from already knowing the answer to that question, it's unrelated since that's about struct members whereas I'm currently asking about struct names in the tags namespace.
FYI: I've been programming in C on-and-off since the 1980s, so I know C. But I've never seen any explanation of Ritchie's rationale for the tags namespace.
I originally asked this question here at Retrocomputing, but it was suggested by Raffzahn and agreed to by Adam Hyland that I ask this question here on Langdev.
linkin C++ to be able to use it self-referentially. $\endgroup$of, followed by zero or more carets, and nothing other than a type name could appear in the places a type name could appear. $\endgroup$