"friend Foo" instead of "friend class Foo"?

Discussion in 'C++' started by Peter, Jun 6, 2013.

  1. Peter

    Peter Guest

    Hi.

    I remember a code snippet from "Thinking in C++" where "class" and
    "struct" keywords were missing from a friend declaration. The example
    was more or less like this (it came from a chapter about nested
    classes and "friend" keyword):

    struct Boo
    {
    struct Foo;
    friend Foo; // neither "struct" nor "class" after "friend"
    struct Foo{};
    };

    Tested with an older version of g++ it resulted in the following
    errors:

    "error: a class-key must be used when declaring a friend"
    "error: friend declaration does not name a class or function"

    However, Comeau handled the above fine. Which result is standard
    compliant in this case? I think an elaborated specifier is usually
    required in friend declarations, but I read somewhere there's an
    exception in presence of forward declarations like in the code above.
    What are the precise rules? When exactly can "struct/class" keywords
    be omitted in friend declarations and how do the most recent versions
    of g++ and MSVC handle this (do they obey the standard or not)?
     
    Peter, Jun 6, 2013
    #1
    1. Advertising

  2. On 6/6/2013 1:14 PM, Peter wrote:
    > I remember a code snippet from "Thinking in C++" where "class" and
    > "struct" keywords were missing from a friend declaration. The example
    > was more or less like this (it came from a chapter about nested
    > classes and "friend" keyword):
    >
    > struct Boo
    > {
    > struct Foo;
    > friend Foo; // neither "struct" nor "class" after "friend"
    > struct Foo{};
    > };
    >
    > Tested with an older version of g++ it resulted in the following
    > errors:
    >
    > "error: a class-key must be used when declaring a friend"
    > "error: friend declaration does not name a class or function"
    >
    > However, Comeau handled the above fine. Which result is standard
    > compliant in this case? I think an elaborated specifier is usually
    > required in friend declarations, but I read somewhere there's an
    > exception in presence of forward declarations like in the code above.
    > What are the precise rules? When exactly can "struct/class" keywords
    > be omitted in friend declarations and how do the most recent versions
    > of g++ and MSVC handle this (do they obey the standard or not)?


    [class.friend]/3 allows it to be a simple-type-specifier. IOW, there
    does not have to be 'class' or 'struct' there, if the type has already
    been declared (like in the case of yours).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 6, 2013
    #2
    1. Advertising

  3. Peter

    Öö Tiib Guest

    On Thursday, 6 June 2013 20:14:04 UTC+3, Peter wrote:
    > I remember a code snippet from "Thinking in C++" where "class" and
    > "struct" keywords were missing from a friend declaration. The example
    > was more or less like this (it came from a chapter about nested
    > classes and "friend" keyword):
    >
    >
    > struct Boo
    > {
    > struct Foo;
    > friend Foo; // neither "struct" nor "class" after "friend"
    > struct Foo{};
    > };


    Looks like valid code.

    Note that the nested class 'Boo::Foo' (as member class) has currently
    access to other members of 'Boo'. IOW that 'friend Foo;' does not grant
    it anything that it does not already have.
     
    Öö Tiib, Jun 6, 2013
    #3
    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. evolve
    Replies:
    2
    Views:
    512
    William Ryan
    Nov 2, 2003
  2. Patricia Vazquez

    Mentor editor instead of ISE

    Patricia Vazquez, Feb 4, 2004, in forum: VHDL
    Replies:
    1
    Views:
    475
    Amontec Team, Laurent Gauch
    Feb 4, 2004
  3. Dan NITA

    lpm_counter instead adders

    Dan NITA, Mar 8, 2005, in forum: VHDL
    Replies:
    4
    Views:
    1,391
    Dan NITA
    Mar 10, 2005
  4. Laurey
    Replies:
    2
    Views:
    442
    Laurey
    Jan 30, 2004
  5. Sara
    Replies:
    0
    Views:
    695
Loading...

Share This Page