Topic: Friends


Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 1999/06/01
Raw View
Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:

> Actually, I don't expect it to do anything at all :) I've used this code
> in a bug report and received a reply that the code is illegal.

The code is well-formed. Both declarations of bar refer to the same
(global) function bar, which is not cannot be called, because it is
not visible globally (only Koenig lookup would find it, but it doesn't
take an argument of either Foo or Bar).

> of the more advanced compilers I have (egcs and Borland's C++Builder)
> reject it

FWIW, egcs 2.95 (which will be gcc 2.95) now accepts it. egcs 1.1
rejects it with a rather strange error message.

Regards,
Martin
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1999/05/29
Raw View
Hi,

consider the following code:

class Foo {
 friend void bar(int&);
};

class Bar {
 friend void bar(int& i) {
  i = 1;
 }
};

Some of my compilers accept it and some reject it. I've searched the
standard but couldn't figure out if it's legal. Could someone please
clarify this for me (preferably with an explanation).

Bye

Roman
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1999/05/29
Raw View
In article <374C74EB.4D9204E3@cs.tu-berlin.de>, wolfro@cs.tu-berlin.de
says...
> Hi,
>
> consider the following code:

It looks to me like it's legal, but I don't think it does quite what
you expect.

>
> class Foo {
>  friend void bar(int&);

This declares that a function named bar that is not a member of a
class is a friend of this class.

> };
>
> class Bar {
>  friend void bar(int& i) {

Here's where things get tricky -- even though this function is defined
inside of the class declaration, it is NOT a member function.  By
putting "friend" in the function definition, this becomes a global
function.  Interestingly enough, even though it's no longer a member
function, defining the function inside of the class declaration DOES
still make it an inline function.

> Some of my compilers accept it and some reject it. I've searched the
> standard but couldn't figure out if it's legal. Could someone please
> clarify this for me (preferably with an explanation).

It's legal.  The grammar for a function definition includes the
possibility of it including "friend", and nothing in section 8.4 says
you can't include it.  The (rather unusual) semantics of "friend" in a
function definition inside of a class declaration is discussed in
section 11.4/4.

Just FWIW, your example didn't really make use of the friendship you
defined.  A better example might be:

class Foo {
 int x;
 friend void bar(Foo &);
};

class Bar {
 friend void bar(Foo &y) {
  // use private member of y...
  y.x = 2;
 }
};

My guess is that you were really trying to create bar() as a member
function of Bar, and wanted that function (and not the rest of Bar) to
be a friend of Foo.  That's also possible and legal, but the syntax is
a bit different:

class Foo2;

class Bar2 {
 void bar(Foo2 &);
// void junk(Foo2 &);
};

class Foo2 {
 int x;
 friend void Bar2::bar(Foo2 &);
};

inline void Bar2::bar(Foo2 &y) { y.x = 2; }
// inline void Bar2::junk(Foo2 &y ) { y.x = 2; }

If you uncomment the declaration and definition of Bar2::junk, the
code should no longer compile, because the friendship was granted only
to Bar2::bar, not to the rest of Bar2.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1999/05/31
Raw View
Jerry Coffin wrote:
>
> In article <374C74EB.4D9204E3@cs.tu-berlin.de>, wolfro@cs.tu-berlin.de
> says...
> > Hi,
> >
> > consider the following code:
>
> It looks to me like it's legal, but I don't think it does quite what
> you expect.

Actually, I don't expect it to do anything at all :) I've used this code
in a bug report and received a reply that the code is illegal. Since two
of the more advanced compilers I have (egcs and Borland's C++Builder)
reject it and the older Sun C++ 4.2 and Cray C++ accept it I thought
there might have been a recent change in the specs. Thanks for your
reply.

Bye

Roman
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]