命名空间成员定义

时间:2010-11-24 13:45:22

标签: c++ namespaces definition member

namespace M{
   void f();
   void M::f(){}
}

int main(){}

上面的代码给出了错误:

  

“ComeauTest.c”,第3行:错误:   不允许使用限定名称   名称空间成员             宣言        void M :: f(){}

  

G ++也会出错。

但是

  

VS2010编译得很好。

我的问题是:

a)预期的行为是什么?

b)$ 7.3.1.2似乎没有谈到这个限制。标准的哪一部分指导了此类代码的行为?

2 个答案:

答案 0 :(得分:5)

  

标准的哪一部分指导此类代码的行为?

C ++ 03 Section $ 8.3说

  

除了类的成员函数(9.3)或静态数据成员(9.4)的定义,函数或命名空间的变量成员的定义或显式实例化之外,不应限定declarator-id。它的名称空间的外部,或者在其名称空间之外的先前声明的显式特化的定义,或者是另一个类或命名空间的成员的友元函数的声明(11.4)。

所以你的代码格式不正确。

然而,在讨论issue 548时,CWG同意禁止在其名称空间内禁止合格的声明者 1

1:活动问题482

答案 1 :(得分:0)

7.3.1.2-2具体谈到这个问题:

Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration’s namespace.

M::f被视为名称空间定义之外的内容。

相关问题