Name spaces in C

↘锁芯ラ 提交于 2020-03-21 08:39:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!