This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

nested class as friend




with compiler:  gcc version egcs-2.90.14 971023 (gcc2-970802 experimental)
on Linux version 2.0.0 on Pentium PC

nested friend class when the friend class is not yet defined
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I want to make a nested class Y::Z a friend of class A.
Sometimes it is not possible to provide the full definition of the
friend class before (e.g. in case of mutual friends).
I reduced the tests to very small cases, each time, I give the
complete source (only a few lines) and the response of the compiler.



test1:
-----------v-------
class A {
   friend class Y::Z;
};
-----------^-------
> g++ -c  -Wall   tst1.cc 
tst1.cc:2: parse error before `;'



test2:
-----------v-------
class Y;

class A {
   friend class Y::Z;
};
-----------^-------
> g++ -c  -Wall   tst1.cc 
tst1.cc:4: `Y' does not have a nested type named `Z'




Test3:
-----------v-------
class Y;
class Z;    // not true, but is a workaround for gcc 2.7.2

class A {
   friend class Y::Z;
};
-----------^-------
> g++ -c  -Wall   tst1.cc 
tst1.cc:5: `Y' does not have a nested type named `Z'

This last version would be fine as long class Y is the only 
class with a subclass Z (ARM Section 18.3.5 p407).
I could even say 
         friend class Z;
However with subclasses like "iterator", many classes have
a subclass with the name "iterator", we should not make them 
all friends.


I compared the results with gcc version 2.7.2 :
Test 1 gives the same response with gcc 2.7.2
Test 2 gives an internal error in   gcc 2.7.2
Test 3 gives no error          with gcc 2.7.2


Thanks for improving gcc in the egcs project

Luc Claeys


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]