问题
The following definition of MyStruct (tag MyStruct) and type definition of type MyStruct seems perfectly compillable by gcc (at least 4.6.2) and by g++.
typedef struct MyStruct {
int a;
int b;
} MyStruct;
My question is: is it somehow error-prone (in C and/or C++) or bad style to use tag name the same as the type name?
According to http://www.eetimes.com/discussion/programming-pointers/4024450/Tag-vs-Type-Names it is not:
I've never understood why they use different names for the tag and the typedef when one name will do just fine:
typedef struct tree_node tree_node;
But often I've seen code styled like:
typedef struct tagMyStruct {...} MyStruct;
- typedef struct myStruct {...} MyStruct;`
typedef struct _MyStruct {...} MyStruct;
< yes, I know about underscore + capital letter
In each case someone went to some extent to make tag name differ from type name. Are there any practical reasons behind it?
Side note: I use C++ compiler but I would like to be C compatible with those definitions (I know this is bad style for c++). For reasons of maintaining usability of some debug tools, I need all the tag names to be meaningful (not the automatically generated __unknown_something tags in case of unnamed structs you can, for example, see in visual studio class view).
Same issue/question apply to unions and enums.
回答1:
Language wise it is perfectly ok both in C and C++, as tag names exist in a separate name space (not namespace
).
In C++ it is definitely bad style to use the typedef
at all, as it is redundant. You can use both MyStruct
and struct MyStruct
whether you have the typedef
or not.
If you think C compatibility is more important than C++ style, that is your choice. :-)
回答2:
The 'struct Foo' declares struct in different namespace. So, 'struct Foo' and 'Foo' cannot interfere.
The reason for doing typedef struct _foo {} foo;
is not very clear for me. Maybe it is relict of pointer (typedef struct _foo {} foo, *Pfoo;
) typedefing.
回答3:
Technically, there is no reason that the names need be different and a great deal of code has been written using the same name for both identifiers. The important thing is to be consistent within your own code.
It can be argued that overusing typedef
pollutes the namespace, and that can be true. The rule-of-thumb that I use is: If I'm going to use it a lot use a typedef
, if not don't.
Also, if I'm using a typedef
I don't name the struct
, so I'll code it just like this:
typedef struct {
int a;
int b;
} struct_t;
That way I have to use it consistently, and consistency is the key to good coding style.
回答4:
Because C (can be) implemented a one-pass compiler, self-referencing the typedef when it is not created is an error. Eg, this is invalid,
typedef struct {
Foo *next;
int i;
} Foo;
thus,
typedef struct tagFoo {
struct tagFoo *next;
int i;
} Foo;
when you want to refer to it as Foo
and not struct Foo
. See Why should we typedef a struct so often in C? for discussion.
来源:https://stackoverflow.com/questions/9534884/c-style-c-correctness-is-struct-union-enum-tag-same-as-type-name-bad-in-any-w