问题
I have the following code:
typedef struct Y {int X;} X;
enum E {X};
which generates a error:
error: 'X' redeclared as different kind of symbol
As I know, C has implicitly defined namespaces for structure, union, and enum tags and also for their members. So, I'm not sure why does E::X
collide with typedef structure tag X
?
What exactly are name spaces in C?
回答1:
C does not have a separate namespace for enum
members. When you write enum {X}
, that creates a global constant X
(which can clash with other global names such as typedef
'd tags).
回答2:
Because the type X
is declared in the global namespace, that then contains enum E
, that, in turn, contains a redeclaration of X
. So this happens because they're not on the same level - one "namespace" contains the other.
来源:https://stackoverflow.com/questions/13545885/name-spaces-in-c