By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

G++ 4: Unrecognised Friendship

P: n/a
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

Aug 10 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
JH Trauntvein wrote:
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
Aug 10 '05 #2

P: n/a
Victor Bazarov wrote:
JH Trauntvein wrote:
[redacted]
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.
Aug 10 '05 #3

P: n/a

Victor Bazarov wrote:
JH Trauntvein wrote:
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.

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

Aug 10 '05 #4

P: n/a
JH Trauntvein wrote:
[...] 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
Aug 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.