extern存储类说明符 [英] extern storage class specifier

查看:672
本文介绍了extern存储类说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准的第7.1节将extern作为存储类说明符提及。

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


N3126 - extern说明符可以
仅应用于变量
和函数的名称extern说明符
不能在
类成员或函数参数的声明中使用
对于链接一个名称为
,带有extern说明符,见3.5。[
注意:extern关键字也可以是在显式实例化中使用的

linkage-specifications,但它不是
a storage-class-specifier in this
contexts。-end note]

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 ]

它在链接规范的上下文中使用,但是我无法掌握使用extern作为存储说明符。

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. 不是所有的extern的名称都有静态存储时间?

  2. 如果1回答是肯定的,那么为什么会出现这种冗余?C兼容性?


推荐答案

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

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.

例如:

int a; // Ex1

extern int b; // Ex2

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

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.

如果 Ex1 Ex2 发生在函数体中,则 a 具有自动存储持续时间和无链接,但 b 将引用具有外部链接和静态存储持续时间的对象。在这个例子中, extern 在链接,存储时间和是否是定义都影响了声明的含义。

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.

最后,在C ++中,这里是一个例子,其中 extern 的唯一效果是改变从内部到外部的链接。

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

这篇关于extern存储类说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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