Why do enumeration constants have no linkage?

寵の児 提交于 2019-12-02 03:56:06

In 6.2.2 4, the standard intends to discuss linkage only for identifiers of objects and functions, but it fails to make this clear.

Enumeration constants are mere values, not objects or functions, and their identifiers never have any linkage.

Observe the declaration extern int a; declares a as an identifier for an int object. An int object is a different thing from an int value, so an enumeration constant named a cannot be the same thing as an int object named a. So the declaration of extern int a; is invalid even before linkage is considered.

An identifier declared as an enumeration constant has type int

that doesn't means it is a variable of type int

but

extern int a;

says there is a variable of type int named a, this is a conflict with the enumeration constant


Why does not enumeration constant have no linkage

for the same reason the constant 123 (also having type int, but whatever) has no linkage too

Linkage does not matter here. In the same compilation unit you try to have two same identifiers Imagine if the code compiles:

enum test {
    a = 1
};

extern int a; 


int b = a;   // which `a`? a as the external variable or `a` as a constant? How to decide.

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