问题
I'm trying to understand linkage of enumeration constant
s and could not find a clear answer in the Standard N1570
. 6.2.2(p6)
:
The following identifiers have no linkage: an identifier declared to be anything other than an object or a function; an identifier declared to be a function parameter; a block scope identifier for an object declared without the storage-class specifier
extern
.
So I need to understand that constants are not objects. Object is defined as 3.15
:
region of data storage in the execution environment, the contents of which can represent values
Also 6.2.2(p4)
(emphasize mine):
For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage.
Anyway 6.4.4.3(p2)
:
An identifier declared as an enumeration constant has type
int
.
Combining all that I don't understand why
enum test {
a = 1
};
extern int a; //compile-error. UB?
does not compile? I expected a
to have external linkage.
LIVE DEMO
Is the behavior well-defined? Can you provide a reference to the Standard explaining that?
回答1:
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.
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/54397735/why-do-enumeration-constants-have-no-linkage