Behavior of friend class in a namespace.

A

Alan Johnson

Consider the following piece of code:

// test.cpp
class B ;

namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}

class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;


g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context

If the friend declaration is changed to:
friend class ::B ;

then g++ 4.1.0 also accepts the code.

I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.
 
R

red floyd

Alan said:
Consider the following piece of code:

// test.cpp
class B ;

namespace N
{
class A
{
friend class B ;
protected:
int i ;
} ;
}

class B
{
N::A a ;
public:
B() { a.i = 42 ; }
} ;


g++ 3.4.6 will compile this without complaint. g++ 4.1.0, however,
gives the error:
test.cpp: In constructor ‘B::B()’:
test.cpp:9: error: ‘int N::A::i’ is protected
test.cpp:17: error: within this context

If the friend declaration is changed to:
friend class ::B ;

then g++ 4.1.0 also accepts the code.

I am trying to determine which (if either) behavior is correct, but I'm
having trouble locating the relevant section of the standard. Any help
would be appreciated.

I believe 4.1 is correct. The friend declaration in the unmodified code
declares N::B to be a friend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top