C++ 声明中的显式限定

explicit qualification in C++ declaration

当第一个声明被注释掉时,以下命名空间定义无法编译。如果 foo 的第一个声明没有注释,那么它编译得很好。

namespace Y
{
    //void foo();
    void ::Y::foo(){}
}

标准(§8.3¶1)中的相关部分说:

When the declarator-id is qualified, the declaration shall refer to a previously declared member

我了解此规则可防止将名称引入其他命名空间。我想知道是否可以放宽该规则以允许 qualified-ids 引用当前命名空间。

CWG #482 相关:

According to 8.3 [dcl.meaning] paragraph 1, […]
This restriction prohibits examples like the following:

void f();
void ::f();        // error: qualified declarator

namespace N {
  void f();
  void N::f() { }  // error: qualified declarator
}

There doesn't seem to be any good reason for disallowing such declarations, and a number of implementations accept them in spite of the Standard's prohibition. Should the Standard be changed to allow them?

Notes from the April, 2006 meeting:

In discussing issue 548, the CWG agreed that the prohibition of qualified declarators inside their namespace should be removed.

因此,如果存在 foo 的第一个声明(大约从 2012 年开始;GCC 有一个 open bug report),那么您的代码是有效的。但是,如果不是,您引用的措辞仍然适用,并使合格声明格式错误。我认为没有理由允许这种情况;它直观地暗示名称已经声明,因为限定名称查找必须确定它指的是什么。