C ++声明中的显式限定 [英] explicit qualification in C++ declaration

查看:546
本文介绍了C ++声明中的显式限定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当第一个声明被注释掉时,以下名称空间定义无法编译.如果foo的第一个声明没有注释,则可以正常编译.

The following namespace definition fails to compile when the first declaration is commented out. If the first declaration of foo is uncommented, then it compiles just fine.

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

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

The relevant part in the standard (§8.3¶1) says:

当声明者ID合格时,声明应引用先前声明的成员

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

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

I understand that this rule prevents the introduction of names into other namespaces. I wonder if that rule could be relaxed to allow for qualified-ids referring to the current namespace.

推荐答案

CWG#482 是相关的:

CWG #482 is relevant:

根据8.3 [dcl.意义]第1款,[…]
此限制禁止出现以下示例:

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?

2006年4月的会议记录:

Notes from the April, 2006 meeting:

在讨论问题548 ,CWG同意禁止 应该删除其命名空间中的合格声明符.

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

因此,如果存在foo的第一个声明,则您的代码有效(自2012年起; GCC具有

So your code is valid if the first declaration of foo is present (as of about 2012; GCC has an open bug report). If not, however, your quoted wording still applies and renders the qualified declaration ill-formed. I see no reason to permit that case; it intuitively implies that the name has been declared already, since qualified name lookup must determine what it refers to.

这篇关于C ++声明中的显式限定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆