关于匿名命名空间的问题...... [英] question about anonymous namespace...

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

问题描述

我正在阅读C ++ Annotations一书,这里是

书中的一句话:


命名空间可以没有名字来定义。这样的命名空间是匿名的,并且它将已定义实体的

可见性限制在定义了

匿名命名空间的源文件中。

匿名命名空间中定义的实体与C?s
静态函数和变量相当。

在C ++中,仍然可以使用static关键字,但它在类定义中的使用更多



(见第6章)。在需要静态变量或函数
的情况下,首选使用

匿名命名空间。

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


可以有人给我一个例子,说明为什么匿名名称空间是

与静态相同C中的变量和函数?b $ b谢谢!

解决方案

Luna Moon写道:
< blockquote class =post_quotes>
我正在阅读C ++ Annotations一书,这里是

书的引用:


可以在没有名称的情况下定义命名空间。这样的命名空间是匿名的,并且它将已定义实体的

可见性限制在定义了

匿名命名空间的源文件中。

匿名命名空间中定义的实体与C?s
静态函数和变量相当。

在C ++中,仍然可以使用static关键字,但它在类定义中的使用更多



(见第6章)。在需要静态变量或函数
的情况下,首选使用

匿名命名空间。


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


可以有人给我一个例子,说明为什么匿名名称空间是

与静态相同; C中的变量和函数



引用说明全部:它限制了定义的

实体对源文件的可见性其中定义了匿名命名空间


这是什么静态在C.


-

Ian Collins


在文章中

< 4b ********************************** @ a29g2000pra。 googlegroups.com> ;, Luna

Moon< lu ********** @ gmail.comwrote:


I我正在阅读C ++ Annotations一书,这里是

书中的一句话:


命名空间可以没有名字来定义。这样的命名空间是匿名的,并且它限定了定义的实体

对定义了匿名命名空间的源文件的可见性。

匿名命名空间中定义的实体与C'的
静态函数和变量相当。在C ++中,static关键字仍然可以使用
,但它的使用在类定义中更常见(参见

第6章)。在需要静态变量或函数的情况下,首选使用匿名命名空间。


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


可以有人给我一个例子,说明为什么匿名名称空间是

与静态相同C中的变量和函数?



为什么?因为标准是这样说的。实际上,效果并不完全相同。


//命名空间之前

static int i;

static void f( ){}

struct my_foo {};


//命名空间后

命名空间

{

int i;

void f(){}

struct foo {}; //名称上不需要前缀,因为它现在是本地名称

}


BTW,正确的术语是未命名的命名空间。 (C ++ 03 7.3.1.1)。您正在阅读的C ++书籍的作者应该花时间来做正确的事。


10月23日,9: 08 am,blargg .... @ gishpuppy.com(blargg)写道:


文章

< 4b97d33c-17f1-4bf8 -a02b-71ca05250 ... @ a29g2000pra.googlegroups.com>,

Luna Moon< lunamoonm ... @ gmail.comwrote:



[...]


可以有人给我一个关于为什么匿名

名称的例子空间与静态相同变量和函数
C中的


为什么?因为标准是这样说的。实际上,效果并不是完全相同的。


//命名空间之前

static int i;

static void f(){ }

struct my_foo {};


//名称空间后

名称空间

{

int i;

void f(){}

struct foo {}; //名称上不需要前缀,因为它现在是本地名称

}



另一个重要的区别是名称。

静态变量或函数具有内部链接。

匿名命名空间中的名称具有外部链接---从其他翻译单元看不出来的原因是它无法在

他们。 (形式上,它随处可见,但因为它不能被命名,所以没有办法真正看到它。)一个模板只能

实例化具有外部链接的东西,

例如:


template< int * p class T {};


static int i;

namespace {

int j;

}


T< & i ti; //非法......

T< & j tj; // legal。


BTW,正确的术语是未命名的命名空间。 (C ++ 03 7.3.1.1)。

你正在阅读的C ++书的作者应该花时间来确定它。



匿名命名空间一词在我看来相当普遍。


-

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


I am reading the book "C++ Annotations", and here is a quote from the
book:

Namespaces can be defined without a name. Such a namespace is
anonymous and it restricts the
visibility of the defined entities to the source file in which the
anonymous namespace is defined.
Entities defined in the anonymous namespace are comparable to C?s
static functions and variables.
In C++ the static keyword can still be used, but its use is more
common in class definitions
(see chapter 6). In situations where static variables or functions are
necessary, the use of the
anonymous namespace is preferred.
--------------

Could anybody give me an example about why the anonymous name space is
the same as "static" variables and functions in C?

thanks!

解决方案

Luna Moon wrote:

I am reading the book "C++ Annotations", and here is a quote from the
book:

Namespaces can be defined without a name. Such a namespace is
anonymous and it restricts the
visibility of the defined entities to the source file in which the
anonymous namespace is defined.
Entities defined in the anonymous namespace are comparable to C?s
static functions and variables.
In C++ the static keyword can still be used, but its use is more
common in class definitions
(see chapter 6). In situations where static variables or functions are
necessary, the use of the
anonymous namespace is preferred.
--------------

Could anybody give me an example about why the anonymous name space is
the same as "static" variables and functions in C?

The quote says it all: "it restricts the visibility of the defined
entities to the source file in which the anonymous namespace is defined"

Which is exactly what "static" does in C.

--
Ian Collins


In article
<4b**********************************@a29g2000pra. googlegroups.com>, Luna
Moon <lu**********@gmail.comwrote:

I am reading the book "C++ Annotations", and here is a quote from the
book:

Namespaces can be defined without a name. Such a namespace is
anonymous and it restricts the visibility of the defined entities
to the source file in which the anonymous namespace is defined.
Entities defined in the anonymous namespace are comparable to C''s
static functions and variables. In C++ the static keyword can still
be used, but its use is more common in class definitions (see
chapter 6). In situations where static variables or functions are
necessary, the use of the anonymous namespace is preferred.

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

Could anybody give me an example about why the anonymous name space is
the same as "static" variables and functions in C?

Why? Because the standard says so. Actually, the effect is not exactly the same.

// Before namespace
static int i;
static void f() { }
struct my_foo { };

// After namespace
namespace
{
int i;
void f() { }
struct foo { }; // no need for prefix on name, since it''s now a local name
}

BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1). The author of
the C++ book you''re reading should have taken the time to get it right.


On Oct 23, 9:08 am, blargg....@gishpuppy.com (blargg) wrote:

In article
<4b97d33c-17f1-4bf8-a02b-71ca05250...@a29g2000pra.googlegroups.com>,
Luna Moon <lunamoonm...@gmail.comwrote:

[...]

Could anybody give me an example about why the anonymous
name space is the same as "static" variables and functions
in C?

Why? Because the standard says so. Actually, the effect is not
exactly the same.

// Before namespace
static int i;
static void f() { }
struct my_foo { };

// After namespace
namespace
{
int i;
void f() { }
struct foo { }; // no need for prefix on name, since it''s now a localname
}

Another important difference is the linkage of the name. A
static variable or function has internal linkage. A name in an
anonymous namespace has external linkage---the reason it can''t
be seen from other translation units is that it isn''t namable in
them. (Formally, it is visible everywhere, but since it can''t
be named, there''s no way to actually see it.) A template can
only be instantiated over something that has external linkage,
e.g.:

template< int* p class T {} ;

static int i ;
namespace {
int j ;
}

T< &i ti ; // illegal...
T< &j tj ; // legal.

BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1).
The author of the C++ book you''re reading should have taken
the time to get it right.

The term anonymous namespace seems pretty widespread to me.

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


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

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