Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

c++ - friend declaration not forward declaring

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Take a look at 11.3 paragraph 11:

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.

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...