匿名命名空间和链接 [英] anonymous namespace and linkage

查看:57
本文介绍了匿名命名空间和链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


AFAIK外部链接允许您在当前翻译单元之外引用变量/函数

。未命名的
命名空间中的变量类似于声明静态变量,但根据

标准,存在差异:

"虽然这在实际效果上基本上是正确的,但是存在微妙的b
差异。使用如此处所示的静态导致i。有内部

的联系。当在未命名的命名空间中声明而不使用

static时,它具有外部链接。这种静态的使用是正式的。不推荐使用
(C ++标准7.3.1.1/2)。"


对我而言,这意味着变量可以在

翻译单位。然而,Herb Schildt的书显示以下内容:

------------------------------ -------------------------------------------------- ----------

虽然在C ++中仍然允许使用静态全局声明,但

更好的方式来获取
完成相同的效果是使用未命名的命名空间。对于

示例:

File One

命名空间{

int k;

}

void f1(){

k = 99; //好吧

}


文件二

extern int k;

void f2() {

k = 10; //错误

}

这里,k也仅限于File One。对于新代码,建议使用未命名的

命名空间而不是

static。

----------- -------------------------------------------------- -----------------------------


那怎么样''k' '有外部联系吗?


谢谢


Taras

解决方案

< blockquote> Taras_96写道:


AFAIK外部链接允许你在当前翻译单元之外引用变量/函数

。未命名的

命名空间中的变量类似于声明静态变量,但根据

标准有一个区别:


虽然这在实际效果中基本上是正确的,但是存在微妙的b
差异。使用如此处所示的静态导致i。有内部

的联系。当在未命名的命名空间中声明而不使用

static时,它具有外部链接。这种静态的使用是正式的。不推荐使用
(C ++标准7.3.1.1/2)。"


对我而言,这意味着变量可以在

翻译单位。



当编译器*另一个*翻译单元时,编译器无法确定

变量的正确名称。这就是诀窍

使它不可用。该名称是唯一的,而

编译器正在编译该特定的翻译单元。


但是,Herb Schildt的书显示了以下:



唉!呸!我在告诉妈妈!妈妈,塔拉斯说S名字,和

他正在读这本书!...

把那本书扔掉,Taras,给自己一本*真正的* C ++书。

访问书籍评论部分的 www.accu.org 看看什么是

推荐的头衔。


[..]


那怎么样那个k有外部联系吗?



就是这样。编译器为它提供了外部链接。

它是如何实现的?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


9月6日晚上8:56,Taras_96< taras ... @ gmail.comwrote:


AFAIK外部联动允许您在当前翻译单元之外引用

变量/函数。未命名的命名空间中的一个

变量类似于声明一个

的静态变量,但根据标准,有一个

的差异:


"虽然这在实际效果中基本上是正确的,但是有b / b
的细微差别。使用如此处所示的静态导致i。

有内部联系。当在未命名的命名空间中声明

而不使用静态时,它具有外部链接。这种使用

of static已被正式弃用(C ++标准7.3.1.1/2)。"


对我而言,这意味着该变量可以在翻译单元的

之外访问。然而,Herb Schildt的书显示了

以下:

---------------------- -------------------------------------------------- ------------------

虽然在C ++中仍然允许使用静态全局声明

,但更好的方法实现相同的效果是使用

一个未命名的命名空间。例如:

File One

命名空间{

int k;}


void f1(){

k = 99; // OK

}


文件二

extern int k;

void f2(){

k = 10; //错误}


这里,k也仅限于File One。对于新代码,建议使用未命名的

名称空间而不是静态名称。

------------------- -------------------------------------------------- ---------------------


那怎么样''k' '有外部联系吗?



它具有外部链接,就像任何其他变量一样。在文件

one中,定义位于名称空间中,其名称对于

文件是唯一的;在文件2中,k在全局命名空间中。这或多或少是
,就像你有的一样:


文件一:

命名空间TopSecrectName {

int k;

}

使用TopSecrectName;

// ...


文件二

extern int k;

// ...


因为这两个k不在相同的命名空间,他们的合格的

名称是不同的,他们只能是不同的实体。


-

James Kanze (GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9个地方Sémard,78210 St.-Cyr-l''coco,法国,+ 33(0)1 30 23 00 34


文章

< 0c ********************************** @ j22g2000hsf。 googlegroups.com>,

Taras_96< ta ****** @ gmail.comwrote:


AFAIK外部链接允许你在当前翻译单元之外引用变量/函数

。未命名的

命名空间中的变量类似于声明静态变量,但根据

标准,存在差异:


虽然这在实际效果中基本上是正确的,但是存在微妙的b
差异。使用如此处所示的静态导致i。有内部

的联系。当在未命名的命名空间中声明而不使用

static时,它具有外部链接。这种静态的使用是正式的。不推荐使用
(C ++标准7.3.1.1/2)。"


对我而言,这意味着变量可以在

翻译单位。 [...]



外部联系也有其他有用的后果。第一个想到的是
,这个未命名的命名空间允许

" local"到翻译单位。它们必须具有外部链接,并且

未命名的命名空间是唯一的方法,可以在翻译单元之外提供它们,因为它们不能是静态的。


Hi everyone,

AFAIK external linkage allows you to refer to variables/functions
outside of the current translation unit. A variable in an unnamed
namespace is similar to declaring a static variable, but according to
the standard there is a difference:
"While this is essentially true in practical effect, there are subtle
differences. Using static as shown here causes "i" to have internal
linkage. When declared in an unnamed namespace without the use of
static, it has external linkage. This use of static is officially
deprecated (C++ Standard 7.3.1.1/2)."

To me this means that the variable is accessible outside of the
translation unit. However, Herb Schildt''s book shows the following:
------------------------------------------------------------------------------------------
While the use of static global declarations is still allowed in C++, a
better way to
accomplish the same effect is to use an unnamed namespace. For
example:
File One
namespace {
int k;
}
void f1() {
k = 99; // OK
}

File Two
extern int k;
void f2() {
k = 10; // error
}
Here, k is also restricted to File One. The use of the unnamed
namespace rather than
static is recommended for new code.
------------------------------------------------------------------------------------------

So how is it that ''k'' has external linkage?

Thanks

Taras

解决方案

Taras_96 wrote:

AFAIK external linkage allows you to refer to variables/functions
outside of the current translation unit. A variable in an unnamed
namespace is similar to declaring a static variable, but according to
the standard there is a difference:
"While this is essentially true in practical effect, there are subtle
differences. Using static as shown here causes "i" to have internal
linkage. When declared in an unnamed namespace without the use of
static, it has external linkage. This use of static is officially
deprecated (C++ Standard 7.3.1.1/2)."

To me this means that the variable is accessible outside of the
translation unit.

There is no way for the compiler to figure out the right name for the
variable when compiler *another* translation unit. That''s the trick
to make it "not available". The name is made up unique while the
compiler is compiling that particular translation unit.

However, Herb Schildt''s book shows the following:

Ugh! Yuck! I''m telling Mom! ...Mom, Taras said the "S" name, and
he is reading THAT book!...

Throw that book away, Taras, and get yourself a *real* C++ book.
Visit www.accu.org, the book review section, to see what are the
recommended titles.

[..]

So how is it that ''k'' has external linkage?

It just does. The compiler provides it with external linkage.
What is the difference how it accomplishes that?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Sep 6, 8:56 pm, Taras_96 <taras...@gmail.comwrote:

AFAIK external linkage allows you to refer to
variables/functions outside of the current translation unit. A
variable in an unnamed namespace is similar to declaring a
static variable, but according to the standard there is a
difference:

"While this is essentially true in practical effect, there are
subtle differences. Using static as shown here causes "i" to
have internal linkage. When declared in an unnamed namespace
without the use of static, it has external linkage. This use
of static is officially deprecated (C++ Standard 7.3.1.1/2)."

To me this means that the variable is accessible outside of
the translation unit. However, Herb Schildt''s book shows the
following:
------------------------------------------------------------------------------------------
While the use of static global declarations is still allowed
in C++, a better way to accomplish the same effect is to use
an unnamed namespace. For example:
File One
namespace {
int k;}

void f1() {
k = 99; // OK
}

File Two
extern int k;
void f2() {
k = 10; // error}

Here, k is also restricted to File One. The use of the unnamed
namespace rather than static is recommended for new code.
------------------------------------------------------------------------------------------

So how is it that ''k'' has external linkage?

It has external linkage, just like any other variable. In file
one, the definition is in a namespace with a name unique to the
file; in file 2, k is in global namespace. This is more or less
the same as if you had:

File One:
namespace TopSecrectName {
int k ;
}
using TopSecrectName ;
// ...

File Two
extern int k ;
// ...

Since the two k aren''t in the same namespace, their qualified
names are different, and they can only be different entities.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


In article
<0c**********************************@j22g2000hsf. googlegroups.com>,
Taras_96 <ta******@gmail.comwrote:

AFAIK external linkage allows you to refer to variables/functions
outside of the current translation unit. A variable in an unnamed
namespace is similar to declaring a static variable, but according to
the standard there is a difference:

"While this is essentially true in practical effect, there are subtle
differences. Using static as shown here causes "i" to have internal
linkage. When declared in an unnamed namespace without the use of
static, it has external linkage. This use of static is officially
deprecated (C++ Standard 7.3.1.1/2)."

To me this means that the variable is accessible outside of the
translation unit. [...]

External linkage has other useful consequences as well. The first that
comes to mind is that the unnamed namespace allows templates that are
"local" to a translation unit. They must have external linkage, and the
unnamed namespace is the only way to provide that WITHOUT making them
accessible outside the translation unit, since they cannot be static.


这篇关于匿名命名空间和链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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