朋友声明不转发声明 [英] friend declaration not forward declaring

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

问题描述

我的理解是,如果 class 说明符, friend 声明也可以用作类的前向声明如下例所示:

My understanding was that a friend declaration could also serve as a forward declaration for a class if the class specifier was used, as in this example:

class A
{
    friend class B;
    B* b;
};

class B {};

int main() {}

但是,g ++(4.6.3和4.7.0)给了我以下错误(g ++-4.7应该支持扩展的朋友声明),而无需使用前向声明:

However, g++ (4.6.3 and 4.7.0) gives me the following error (g++-4.7 should have support for extended friend declarations), which is expected without a forward declaration:


main.cpp:6:2:错误: B不命名类型

main.cpp:6:2: error: ‘B’ does not name a type

试图确认我对朋友B级的期望; 应该用作前向声明,我发现此答案这个答案,但是都不是结论性的(或者至少不能从他们那里得出很多结论),因此我尝试参考c ++ 11标准并找到了以下示例:

In an attempt to confirm my expectations that the friend class B; should serve as a forward declaration, I found this answer and this answer, but neither was conclusive (or I couldn't conclude much from them at least) so I attempted to consult the c++11 standard and found this example:

class X2 {
    friend Ct; // OK: class C is a friend
    friend D; // error: no type-name D in scope
    friend class D; // OK: elaborated-type-specifier declares new class
}

根据我的阅读在第三个声明中,我的朋友类B 应该是声明新类的修饰类型说明符

Based on my reading of the the third declaration, my friend class B should be an elaborated-type-specifier declaring a new class.

我才刚刚开始理解官方的标准措辞,所以我肯定缺少一些东西。我误解了什么?

I am just starting to understand official standard wording, so I must be missing something. What am I misunderstanding?

推荐答案

您的朋友B级; 声明确实用作前向声明,但是在提供匹配的声明之前,名称查找不会找到这种声明。

Your friend class B; declaration does serve as a forward declaration, but such declaration is not found by name lookup until a matching declaration is provided.

[class。朋友] / 11


如果朋友声明出现在本地类(9.8)中,并且指定的名称不合格名称,在不考虑最内层非类作用域之外的作用域的情况下,查找先前的声明。对于朋友函数声明,如果没有事先声明,则程序格式错误。对于朋友类声明,如果没有先前的声明,则指定的类属于最里面的封闭的
非类作用域,但如果随后引用了该类,则在匹配之前通过名称查找找不到其名称。声明在最内层的非类作用域中提供。

If a friend declaration appears in a local class (9.8) and the name specified is an unqualified name, a prior declaration is looked up without considering scopes that are outside the innermost enclosing non-class scope. For a friend function declaration, if there is no prior declaration, the program is ill-formed. For a friend class declaration, if there is no prior declaration, the class that is specified belongs to the innermost enclosing non-class scope, but if it is subsequently referenced, its name is not found by name lookup until a matching declaration is provided in the innermost enclosing nonclass scope.

这篇关于朋友声明不转发声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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