G++ 4: Unrecognised Friendship

J

JH Trauntvein

Consider the following example:

namespace n1
{
class cn1_base;

namespace n1_helpers
{
class helper1
{
private:
int private_member;
friend class cn1_base;
};
};

class cn1_base
{
public:
void foo()
{
n1_helpers::helper1 helper;
helper.private_member = 1;
}
};

};

While this compiled with earlier versions of G++, G++ version 4.x fails
to compile this and gives the following message:

'int n1::n1_helpers::helper1::private_member' is prviate within this
context

Fortunately, I can work around this by changing the friend declaration
to the following:

friend class n1::n1_base;

Is this a bug in the compiler or have I misunderstood something about
friendship declarations?

Regards,

Jon Trauntvein
 
V

Victor Bazarov

JH said:
Consider the following example:

namespace n1
{
class cn1_base;

namespace n1_helpers
{
class helper1
{
private:
int private_member;
friend class cn1_base;
};
};

class cn1_base
{
public:
void foo()
{
n1_helpers::helper1 helper;
helper.private_member = 1;
}
};

};

While this compiled with earlier versions of G++, G++ version 4.x fails
to compile this and gives the following message:

'int n1::n1_helpers::helper1::private_member' is prviate within this
context

Fortunately, I can work around this by changing the friend declaration
to the following:

friend class n1::n1_base;

Is this a bug in the compiler or have I misunderstood something about
friendship declarations?

7.3.1.2/3. I think it says essentially that the name declared in a friend
declaration is presumed to exist at the same namespace level as the class
in which it is declared. IOW, since 'helper1' is in 'n1::n1_helpers'
namespace, when it declares 'cn1_base' as its friend, the compiler assumes
that 'cn1_base' is the name of a class in 'n1::n1_helpers' namespace.

V
 
R

red floyd

Victor said:
7.3.1.2/3. I think it says essentially that the name declared in a friend
declaration is presumed to exist at the same namespace level as the class
in which it is declared. IOW, since 'helper1' is in 'n1::n1_helpers'
namespace, when it declares 'cn1_base' as its friend, the compiler assumes
that 'cn1_base' is the name of a class in 'n1::n1_helpers' namespace.

V

I gave him the same answer, and he didn't like it.
 
J

JH Trauntvein

Victor said:
7.3.1.2/3. I think it says essentially that the name declared in a friend
declaration is presumed to exist at the same namespace level as the class
in which it is declared. IOW, since 'helper1' is in 'n1::n1_helpers'
namespace, when it declares 'cn1_base' as its friend, the compiler assumes
that 'cn1_base' is the name of a class in 'n1::n1_helpers' namespace.


Thank you for your reply. I am not wishing to argumentative here but I
feel that there is some potential ambiguity either in the standard or
in my understanding of it :)

The wording that the standard uses in the case of friendship
declarations is that the class will be assumed to be in the most local
namespace if the class is "first declared". I am unsure about what
"first declared" means but would interpret the forward declaration of
class cn1_base as being the "first declaration" and that, because of
this, the clause that you cite should not have any effect.

Regards,

Jon Trauntvein
 
V

Victor Bazarov

JH said:
[...] I am not wishing to argumentative here but I
feel that there is some potential ambiguity either in the standard or
in my understanding of it :)
[..]

Then I recommend seeking advice from comp.std.c++. That's where the
Standards Committee members hang out and that's where the language lawyers
dispense their wisdom.

V
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top