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]
Other format: [Raw text]

[Bug c++/32204] friend from global namespace in template class ignored


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32204

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-03-26 23:45:55 UTC ---
(In reply to comment #3)
> I have run into this bug or a similar bug while porting my code from MSVC to
> GCC.

I don't think you have, I think GCC is right to reject your code and MSVC was
wrong to accept it.

> I need to declare that a class in the global namespace is a friend of my
> template class. However, I discovered that the bug still occurs if no templates
> are involved.

Because you haven't hit this bug :)

> Here's my repro:
> 
> class Friend;
> namespace Foo
> {
>     class Base {
>     };
>     class Derived : protected Base {
>         friend class Friend;

The standard says this declares Friend as a member of the "innermost non-class
scope" which is namespace Foo.

>     };
> }
> class Friend {
>     void F(const Foo::Derived* data) const 
>     {
>         // error: 'Foo::Base' is an inaccessible base of 'Foo::Derived'
>         const Foo::Base* dataB = data;
>     }
> };
> 
> The problem disappears if either (A) Derived is not in a namespace, 

Because then the innermost enclosing non-class scope is the global namespace,
so the same ::Friend class is found.

> or (B)
> Friend is in a namespace (the forward declaration of Friend is required and, of
> course, must be put in the namespace too).

Because then you define the same class as declared by the friend declaration.


To fix your code you should change the friend declaration to refer to ::Friend

        friend class ::Friend;

Or in C++11

        friend Friend;

In both these forms the friend declaration refers to the already-declared
::Friend rather than declaring a new Foo::Friend class.


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