开发者

Namespace member definition

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

int main(){}

The above code gives 开发者_运维知识库error like so:

"ComeauTest.c", line 3: error: qualified name is not allowed in namespace member declaration void M::f(){}

And

G++ also gives error.

But

VS2010 compiles fine.

My questions are:

a) What is the expected behavior?

b) $7.3.1.2 does not seem to talk about this restriction. Which portion of the Standard guides the behavior of such code?


Which portion of the Standard guides the behavior of such code?

C++03 Section $8.3 says

A declarator-id shall not be qualified except for the definition of a member function (9.3) or static data member (9.4) out-side of its class, the definition or explicit instantiation of a function or variable member of a namespace out-side of its namespace, or the definition of a previously declared explicit specialization outside of its name-space, or the declaration of a friend function that is a member of another class or namespace (11.4).

So your code is ill-formed.

However in discussing issue 548 the CWG agreed that the prohibition of qualified declarators inside their namespace should be lifted1.

1 : Active Issue 482


7.3.1.2-2 talks specifically about this:

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 is considered a outside of namespace definition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜