为什么枚举常量没有链接? [英] Why do enumeration constants have no linkage?

查看:102
本文介绍了为什么枚举常量没有链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解enumeration constant的链接,但在标准N1570中找不到明确的答案. 6.2.2(p6):

I'm trying to understand linkage of enumeration constants and could not find a clear answer in the Standard N1570. 6.2.2(p6):

以下标识符没有链接:声明为 除对象或功能以外的任何事物;声明的标识符 作为函数参数;对象的块作用域标识符 声明时没有存储类说明符extern.

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.

所以我需要了解常量不是对象.对象定义为3.15:

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

6.2.2(p4)(强调我的):

对于在存储类说明符extern中声明的标识符 可见该标识符的事先声明的范围,31) 如果先前声明指定内部或外部链接,则 后面声明中标识符的链接与 事先声明中指定的链接. 如果没有事先声明 可见,或者如果先前的声明未指定任何链接,则 标识符具有外部链接.

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.

反正6.4.4.3(p2):

声明为枚举常量的标识符的类型为int.

结合所有我不明白的原因

Combining all that I don't understand why

enum test {
    a = 1
};

extern int a; //compile-error. UB?

不编译吗?我希望a具有外部链接.

does not compile? I expected a to have external linkage.

实时演示

该行为是否定义明确?您可以提供对标准的解释说明吗?

Is the behavior well-defined? Can you provide a reference to the Standard explaining that?

推荐答案

在6.2.2 4中,该标准旨在仅讨论对象和函数的标识符的链接,但未能阐明这一点.

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.

观察声明extern int a;a声明为int对象的标识符. int对象与int值是不同的东西,因此名为a的枚举常量不能与名为aint对象相同.因此,即使在考虑链接之前,extern int a;的声明也是无效的.

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.

这篇关于为什么枚举常量没有链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆