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() {}
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: error: ‘B’ does not name a type
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-speci?er declares new class
}
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…