Topic: Q. friend function


Author: Masao Morita <m-morita@pdss7.trc.rwcp.or.jp>
Date: 2000/03/17
Raw View
Hi,

  I have a question about a friend function.
The standard(ISO/IEC 14482) says
 3.3.1p6[Note: friend declaration refer to functions or classes that of
 nearest enclosing namespace. but the do not introduce new names into
 that namespace. ... ].

But, the other chapter shows me an example as follows(11.4 p5)

  class M {
      friend void f() { }   // definition of global f, a friend of M,
                            // not the definition of a member function

  };

The comment said the above declaration is the definition of global f.
I think it is a mistake. Am I missing something?

I think a friend function which has a definition should have any arguments.
Because, such a function is only referred by argument dependent name lookup.
Am I correct?

Thanks in advance.

M. Morita

---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/18
Raw View
In article <t0taejxu9rr.fsf@pdss7.trc.rwcp.or.jp>, Masao Morita <m-
morita@pdss7.trc.rwcp.or.jp> writes
>Hi,
>
>  I have a question about a friend function.
>The standard(ISO/IEC 14482) says
> 3.3.1p6[Note: friend declaration refer to functions or classes that of
> nearest enclosing namespace. but the do not introduce new names into
> that namespace. ... ].

Notice that this speaks of a declaration not a definition.

>
>But, the other chapter shows me an example as follows(11.4 p5)
>
>  class M {
>      friend void f() { }   // definition of global f, a friend of M,
>                            // not the definition of a member function

assuming that M is at global scope that comment would seem to be
correct.

>
>  };
>
>The comment said the above declaration is the definition of global f.
>I think it is a mistake. Am I missing something?

Why do you think that?

>
>I think a friend function which has a definition should have any arguments.
>Because, such a function is only referred by argument dependent name lookup.
>Am I correct?

Well, of course, the function as written is largely unusable and does
nothing even where you can use it.  However it was only for the purpose
of exemplifying where the name 'f' should be (perhaps not very well). Of
course f could be called from anywhere inside the class scope where it
is visible. It would also bind to a global declaration if M was at
global scope (which it was in the context of the example).


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/03/20
Raw View
Masao Morita wrote:
>
>   I have a question about a friend function.
> The standard(ISO/IEC 14482) says
>  3.3.1p6[Note: friend declaration refer to functions or classes that of
>  nearest enclosing namespace. but the do not introduce new names into
>  that namespace. ... ].
>
> But, the other chapter shows me an example as follows(11.4 p5)
>
>   class M {
>       friend void f() { }   // definition of global f, a friend of M,
>                             // not the definition of a member function
>
>   };
>
> The comment said the above declaration is the definition of global f.
> I think it is a mistake. Am I missing something?

The second example defines a global function f, but does not insert the
declaration into the global namespace. That is, the declaration of the
global f is visible only from the scope of M.

It is similar to declaring a function inside another function:

void f()
{
 void g(); // declare global function
 g(); // OK, call global g
}

void h()
{
 g(); // error, no g is visible
}

void g() { ... } // define and declare the same g declared inside f

void k()
{
 g(); // OK, g is visible
}


--
Steve Clamage, stephen.clamage@sun.com

---
[ 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: Rob Stewart <donotspamme@giage.com>
Date: 2000/03/20
Raw View
Francis Glassborow wrote:
>
> In article <t0taejxu9rr.fsf@pdss7.trc.rwcp.or.jp>, Masao Morita <m-
> morita@pdss7.trc.rwcp.or.jp> writes
> >
> >I think a friend function which has a definition should have any arguments.
> >Because, such a function is only referred by argument dependent name lookup.
> >Am I correct?
>
> Well, of course, the function as written is largely unusable and does
> nothing even where you can use it.  However it was only for the purpose
> of exemplifying where the name 'f' should be (perhaps not very well). Of

f() could be useful if you depend on its side effects.  Perhaps
it checks invariants on something and throws an exception when
there's a problem.

--
Robert Stewart     |  rob-at-giage-dot-com
Software Engineer  |  using std::disclaimer;
Giage, Ltd.        |  http://www.giage.com

---
[ 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: Masao Morita <m-morita@pdss7.trc.rwcp.or.jp>
Date: 2000/03/20
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

> >The standard(ISO/IEC 14482) says
> > 3.3.1p6[Note: friend declaration refer to functions or classes that of
> > nearest enclosing namespace. but the do not introduce new names into
> > that namespace. ... ].
>
> Notice that this speaks of a declaration not a definition.
Thank you for pointing out.
I felt as if that includes a definition! There are some reasons about that.
g++ and KAIC++ (with no special option) are accept the following:

  class M {
     friend void f() { }
  };

  void foo() {
    f();
  }

But KAIC++(3.4g) with --strict option rejects the above program. It says
"f() is undefined". I usually thought it is better for the standard.
But in this case, it is not correct.


> >  class M {
> >      friend void f() { }   // definition of global f, a friend of M,
> >                            // not the definition of a member function
>
> assuming that M is at global scope that comment would seem to be
> correct.
Afterall,

  class M {
      friend void f() { }   // definition of global f, a friend of M,
                            // not the definition of a member function
  };

  is the same as follows:

  class M {
      friend void f();
  };
  void f() { }

The definition of f() will be take place immediately after the definition
of class M.

Thank you so much.

M. Morita

---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/21
Raw View
In article <38D299BD.1D78244E@giage.com>, Rob Stewart
<donotspamme@giage.com> writes
>f() could be useful if you depend on its side effects.  Perhaps
>it checks invariants on something and throws an exception when
>there's a problem.

But as written it was an inline function that did nothing.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: Hideaki Onaru <gomarine@mx1.ttcn.ne.jp>
Date: 2000/03/21
Raw View
> >The standard(ISO/IEC 14482) says
> > 3.3.1p6[Note: friend declaration refer to functions or classes that of
> > nearest enclosing namespace. but the do not introduce new names into
> > that namespace. ... ].
> Notice that this speaks of a declaration not a definition.
But a definition is a kind of declaration, isn't it?

> >But, the other chapter shows me an example as follows(11.4 p5)
> >
> >  class M {
> >      friend void f() { }   // definition of global f, a friend of M,
> >                            // not the definition of a member function
> assuming that M is at global scope that comment would seem to be
> correct.
> >  };
> >
> >The comment said the above declaration is the definition of global f.
> >I think it is a mistake. Am I missing something?
> Why do you think that?
I'm not him, but, if he assumes that every definition introduces a new name,
the comment seems to be against the note of 3.3.1/6.
But, I don't think such an assumption is correct:

"The name declared by a declaration are introduced into the scope in which the
declaration occurs, except that the presence of a friend specifier, certain
uses of the elaborated-type-specifier and using-directives alter this general
behavior." (3.3/3, this quote is from Draft of Dec, 1996)

It seems to me that the declaration of f is a definition (hence is a
declaration at the same time), but that it doesn't introduce the name f into a
global scope.

> >I think a friend function which has a definition should have any arguments.
> >Because, such a function is only referred by argument dependent name lookup.
> >Am I correct?
Why is such a function found only by argument dependent name lookup?
Because of the absence of a declaration of its name in a global scope?
(that is, it is assumed that a friend function defined in a class cannot de
declared outside of the class?
For example,

class M { friend void f() {} };
void f();   // declared in a global scope
void g() {
    f();    // maybe refers to ::f, whose body is not in a global scope, though
}

this example is excluded from what is meant by "a friend function which has a definition"?)

I think the role of a friend-declared function's function-body in a class is
similar to that of a static const integral/enumeration data member's
initializer in a class.


Hideaki Onaru

---
[ 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: Rob Stewart <donotspamme@giage.com>
Date: 2000/03/22
Raw View
Steve Clamage wrote:
>
> Masao Morita wrote:
> >
> >  3.3.1p6[Note: friend declaration refer to functions or classes that of
> >  nearest enclosing namespace. but the do not introduce new names into
> >  that namespace. ... ].
> >
> > But, the other chapter shows me an example as follows(11.4 p5)
> >
> >   class M {
> >       friend void f() { }   // definition of global f, a friend of M,
> >                             // not the definition of a member function
> >   };
> >
> > The comment said the above declaration is the definition of global f.
> > I think it is a mistake. Am I missing something?
>
> The second example defines a global function f, but does not insert the
> declaration into the global namespace. That is, the declaration of the
> global f is visible only from the scope of M.

Does that mean that if "void f()" had been declared prior to M,
the definition of that function could be put in M *and* the name
would be visible in the global namespace?

--
Robert Stewart     |  rob-at-giage-dot-com
Software Engineer  |  using std::disclaimer;
Giage, Ltd.        |  http://www.giage.com

---
[ 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: Rob Stewart <donotspamme@giage.com>
Date: 2000/03/22
Raw View
Francis Glassborow wrote:
>
> In article <38D299BD.1D78244E@giage.com>, Rob Stewart
> <donotspamme@giage.com> writes
> >f() could be useful if you depend on its side effects.  Perhaps
> >it checks invariants on something and throws an exception when
> >there's a problem.
>
> But as written it was an inline function that did nothing.

Oh, right.  I just looked at it again and saw that.  Oh, well.  I
tried.

--
Robert Stewart     |  rob-at-giage-dot-com
Software Engineer  |  using std::disclaimer;
Giage, Ltd.        |  http://www.giage.com

---
[ 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              ]