Search code examples
cenumslanguage-lawyerextern

Why do enumeration constants have no linkage?


I'm trying to understand linkage of enumeration constants 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?


Solution

  • 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.