G++ 4: Unrecognised Friendship

Discussion in 'C++' started by JH Trauntvein, Aug 10, 2005.

  1. 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
    JH Trauntvein, Aug 10, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 10, 2005
    #2
    1. Advertising

  3. JH Trauntvein

    red floyd Guest

    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.
    red floyd, Aug 10, 2005
    #3
  4. 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
    JH Trauntvein, Aug 10, 2005
    #4
  5. 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
    Victor Bazarov, Aug 10, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. red floyd
    Replies:
    0
    Views:
    402
    red floyd
    Aug 4, 2005
  2. Poor Yorick

    __future__ and unrecognised flags

    Poor Yorick, Dec 12, 2008, in forum: Python
    Replies:
    0
    Views:
    255
    Poor Yorick
    Dec 12, 2008
  3. Poor Yorick
    Replies:
    0
    Views:
    264
    Poor Yorick
    Dec 13, 2008
  4. Steve Holden
    Replies:
    1
    Views:
    387
    Rhamphoryncus
    Dec 14, 2008
  5. SteveR

    request format unrecognised

    SteveR, Apr 25, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    106
    SteveR
    Apr 25, 2005
Loading...

Share This Page