C++标准那个文档编写得也太不尽业了

Jinhao 2005-05-19 11:41:54
RT
ISO/IEC 14882:1998
refer to 3.5.4

4 A name having namespace scope has external linkage if it is the name of
— a namespace (7.3), unless it is declared within an unnamed namespace.

...全文
158 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
楚牛9 2005-05-20
  • 打赏
  • 举报
回复
看来你非的把C++搞熟了???
whyglinux 2005-05-20
  • 打赏
  • 举报
回复
说的是这样一种情况:如果name指的是一个namespace名(不妨称之为NInner),并且NInner又定义在一个namespace(N)中,那么如果其定义所在的namespace N不是一个无名namespace,则NInner这个name具有外部连接(属性)。

没有感到什么不妥。
whyglinux 2005-05-20
  • 打赏
  • 举报
回复
从C++标准 3.5 上可以得到这样一个结论:一个name只要它不是一个namespace名,那么它的连接属性就不受它所在的namespace是有名的还是无名的影响。因此,

namespace{
char buffer[256];
};

中的buffer仍然具有外部连接属性。

在此,你应用 3.5.2 所作的判断是不正确的。在使用类(包括模板类)来定义对象的时候,类的定义总是被#include到当前编译单位的,即class X的定义和 X<buffer> x; 其实是在一个编译单位中。当然,要使用 buffer 这个定义在无名空间中的 name ,这个无名空间的定义也必须要在当前编译单位才行。也就是说上述三者都在同一个编译单位中,所以 buffer 的连接属性是内部的还是外部的依据 3.5.2 无从判断。

自从C++引入namespace的概念之后,一个 name 就有了两层限制:一个是声明限制,一个是 namespace 的限制。要访问一个名字,两者缺一不可(全局namespace除外)。所以定义在无名空间中的 name 不能在其它编译单位中使用是因为在其它编译单位中给不出这个 name 的 namespace,而不是声明(包含了连接属性)上的限制。
Jinhao 2005-05-20
  • 打赏
  • 举报
回复
to whyglinux(山青水秀):
感觉上是没什么不妥的。看是看看下面这个代码
namespace{
char buffer[256];
}
Refer to 3.5.2
A name is said to have linkage when it maight denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope:
- When a name has external linkage, the entity it denotes can be referred to bye names form scopes of other translation units or form other scopes of the same translation unit.
- When a name has internal linkage, the entity it denotes can be referred to by names form other scopes in the same translation unit.

按照标准上的意思,那么buffer就是internal linkage,因为这个buffer不能在其他的translation unit当中被引用。
但事实上又并不是这样
template<char * P>
class X{};

X<buffer> x; //可以编译通过!

Refer to 14.3.2.1
A template-augument for a non-type, non-template template-parameter shall be on of:
- the name of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members, expressed as id-expression;
按照标准上的说法,X<buffer> x;可以编译通过,是因为buffer是external linkage.

两点矛盾了~~~

我用Google狗到了下面的一段话来解释这个问题。
an unnamed namespace restricts the visibility of its members to the scope of the translation unit by means of name mangling, it doesn't necessarily guarantee internal linkage
UnrealImagecoffee 2005-05-20
  • 打赏
  • 举报
回复
又 见到你了

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧