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

P

Peter

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)?
 
V

Victor Bazarov

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
 
Ö

Öö Tiib

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.
 

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,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top