Friend's declaration does not announce forward

My understanding was that a friend declaration could also serve as a direct 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() {} 

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 direct declaration:

main.cpp: 6: 2: error: 'B does not name type

In an attempt to confirm my expectations that friend class B; should act as a direct declaration, I found this answer and this answer , but none of them was final (or I could not conclude a lot from them, at least), so I tried to refer to 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 } 

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

I'm just starting to understand the official standard wording, so something is missing me. What? I do not understand?

+7
c ++ c ++ 11
Jan 01 '13 at
source share
2 answers

Your ad friend class B; serves as a direct ad, but no such ad was found by search by name until a corresponding ad is provided.

[class.friend] / 11:

If a friend’s declaration appears in the local class (9.8) and the specified name is an unqualified name, the preliminary announcement is scanned without regard to areas that are outside the innermost encompassing non-classical area. To declare a friend function, if there is no prior announcement, the program is poorly formed. To declare a friend class, if there is no previous declaration, the specified class belongs to the innermost non-classical area, but if you subsequently refer to it, its name will not be searched by name until a corresponding announcement is presented in the innermost encompassing area of ​​the non-class.

+5
Jan 01 '13 at 23:05
source share

Take a look at clause 11.3 of clause 11:

To declare a class of friends, if there is no previous declaration, the specified class refers to the innermost encompassing nonclassical region, but if subsequently referenced, its name will not be found by name until a corresponding declaration is provided in the innermost enclosing nonclassical region.

Example:

 class X; void a(); void f() { class Y; extern void b(); class A { friend class X; // OK, but X is a local class, not ::X friend class Y; // OK friend class Z; // OK, introduces local class Z. friend void a(); // error, ::a is not considered friend void b(); // OK friend void c(); // error }; X *px; // OK, but ::X is found Z *pz; // error, no Z is found } 
+6
Jan 01 '13 at 23:06
source share



All Articles