gpt4 book ai didi

c++ - C++ 声明中的显式限定

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:08 26 4
gpt4 key购买 nike

当第一个声明被注释掉时,以下命名空间定义无法编译。如果 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-id 引用当前命名空间。

最佳答案

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),您的代码就是有效的。但是,如果不是,您引用的措辞仍然适用,并使合格声明格式错误。我认为没有理由允许这种情况;它直观地暗示名称已经声明,因为限定名称查找必须确定它指的是什么。

关于c++ - C++ 声明中的显式限定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33642510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com