外部存储类说明符

发布于 2024-09-28 13:34:02 字数 395 浏览 1 评论 0原文

C++ 标准第 7.1 节提到“extern”作为存储类说明符。

N3126 - “外部说明符可以是 仅适用于变量名称 和功能。外部说明符 不能在声明中使用 类成员或函数参数。 对于声明的名称的链接 带有 extern 说明符,请参见 3.5。 [ 注意:extern关键字也可以是 用于显式实例化和 链接规范,但它不是 此类中的存储类说明符 上下文。 ——尾注]

我了解这个关键字及其在“链接规范”上下文中的使用,但我无法掌握“extern”作为存储说明符的使用。

  1. 不是所有“外部”名称都有静态存储持续时间吗?
  2. 如果对 1 的回答是肯定的,那么为什么会有这种冗余呢? C 兼容性?

Section 7.1 of the C++ Standard mentions about 'extern' as a storage class specifier.

N3126 - "The extern specifier can be
applied only to the names of variables
and functions. The extern specifier
cannot be used in the declaration of
class members or function parameters.
For the linkage of a name declared
with an extern specifier, see 3.5. [
Note: The extern keyword can also be
used in explicit-instantiations and
linkage-specifications, but it is not
a storage-class-specifier in such
contexts. —end note ]

I understand about this keyword and it's use in the context of 'linkage specification', but I am unable to get a grasp on the use of 'extern' as a storage specifier.

  1. Don't all 'extern' names have static storage duration?
  2. If Answer to 1 is yes, then why this redundancy? C Compatibility?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南…巷孤猫 2024-10-05 13:34:02

extern 是一个存储类说明符。这只是语言语法的一个事实。 extern 对程序的语义有多种影响,具体取决于程序的使用位置。它并不是在任何地方都具有相同的效果。它影响对象的存储持续时间和链接,并且还有助于确定某些声明是否也是定义。

例如:

int a; // Ex1

extern int b; // Ex2

例如,如果 Ex1Ex2 在全局范围内,那么它们都将引用具有静态存储持续时间和外部链接的对象。但在 C++ 中,第一个是定义(C 中的暂定定义),而第二个则不是。在此示例中,extern 并未更改所声明对象的存储持续时间或链接。

如果 Ex1Ex2 出现在函数体中,则 a 将引用具有自动存储持续时间且没有链接的对象,但 b< /code> 将引用具有外部链接和静态存储持续时间的对象。在此示例中,extern 影响了声明在链接、存储持续时间以及是否是定义方面的含义。

最后,在 C++ 中,这是一个示例,其中 extern 的唯一作用是将链接从内部更改为外部。

const int c = 5; // static storage duration, internal linkage

extern const int d = 10; // static storage duration, external linkage

extern is a storage class specifier. This is just a fact of the language grammar. extern has a number of effects on the semantics of a program depending on where it is used. It doesn't have the single same effect everywhere. It influences the storage duration and linkage of objects and it also helps determine whether some declarations are also definitions or not.

E.g.:

int a; // Ex1

extern int b; // Ex2

For example, if Ex1 and Ex2 where at global scope then they would both refer to objects with static storage duration and external linkage. In C++, though, the first would be a definition (tentative definition in C) and the second would not. In this example extern has not changed the storage duration or linkage of the declared object.

If Ex1 and Ex2 occurred in a function body then a would refer to an object with automatic storage duration and no linkage but b would refer to an object with external linkage and static storage duration. In this example, extern has affected the meaning of the declaration in both linkage, storage duration and whether or not it is a definition.

Finally, in C++, here is an example where the only effect of extern is changing the linkage from internal to external.

const int c = 5; // static storage duration, internal linkage

extern const int d = 10; // static storage duration, external linkage
时光匆匆的小流年 2024-10-05 13:34:02

它本身并不是真正的存储说明符。它位于变量名称之前,就像其他存储说明符一样,但它所做的只是关闭编译器并告诉链接器它还有更多工作要做。

It's not really a storage specifier per se. It comes before the variable name much as other storage specifiers do, but all it does is shut the compiler up and tell the linker that it has more work to do.

泪冰清 2024-10-05 13:34:02

extern 关键字通知编译器存在 extern 变量(或函数),即使编译器无法在当前正在编译的文件中找到它
它强制外部链接

Extern keyword inform compiler that a extern variable is exists (or function), even if the compiler not able to find it in the file currently being compiled
It forces external linkage

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文