Topic: friend maybe-declaration


Author: wmm@fastdial.net
Date: 1999/07/20
Raw View
In article <3792669c.45090402@news.csrlink.net>,
  jpotter@falcon.lhup.edu (John Potter) wrote:
>
> The new rules about friend declarations not really declaring a
> function in the enclosing namespace, but only declaring that if
> there is a function there, it is a friend.
>
> template<class T> void f (T& x) {
>     x.i = 6;
>     }
> class A {
>     friend void f (A&); // non-template friend
>     int i;
>     };
> void (*pf)(A&) = f; // #1
> void g () {
>     A a;
>     f(a); // #2
>     }
>
> #1 Should this be an error because there is no real declaration of f?

No, this refers to f<A>(A&).  See 13.4p2.  (The non-template function
is not visible to this lookup.)

> #2 Should the template be instantiated because there is no declaration
> for the non-template function available?  It would not be a friend
> and would produce an error, but that is not the question.  Does the
> friend non-declaration prevent the instantiation and allow finding
> a non-template function in another translation unit?

This call refers to the non-template function f(A&) declared in
the friend declaration.  The friend declaration is found by Koenig
lookup (3.4.2) because the function argument is of type A, causing
A to be searched for friend declarations.  It is then selected by
overload resolution because a non-template function is preferred
over a template function that is otherwise an equivalent match
(13.3.3p1).

A function that is declared only in a friend declaration is
completely invisible except for Koenig lookup.

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: fvali@biotrack.com
Date: 1999/07/20
Raw View
In article <3792669c.45090402@news.csrlink.net>,
  jpotter@falcon.lhup.edu (John Potter) wrote:
>
> The new rules about friend declarations not really declaring a
> function in the enclosing namespace, but only declaring that if
> there is a function there, it is a friend.
>
> template<class T> void f (T& x) {
>     x.i = 6;
>     }
> class A {
>     friend void f (A&); // non-template friend
>     int i;
>     };
> void (*pf)(A&) = f; // #1
> void g () {
>     A a;
>     f(a); // #2
>     }
>
> #1 Should this be an error because there is no real declaration of f?
I believe it will be an error, but not because name-lookup would find no
'f' but because implicit instantiation of the function template would
result in an access violation of A's privates, and hence be ill-formed.

Template argument deduction would deduce T to be 'A' and taking its
address would cause its implicit instantiation.
Since 'f' is not used as a postfix expression in a function call, no
namespaces or classes are associated with it (i.e. koenig lookup does
not apply), and so normal unqualified name-lookup is carried out when
searching for declarations that match 'f'.
Since the friend declaration is not found by ordinary name lookup it is
not considered in the set of overloads.


>
> #2 Should the template be instantiated because there is no declaration
> for the non-template function available?  It would not be a friend
> and would produce an error, but that is not the question.  Does the
> friend non-declaration prevent the instantiation and allow finding
> a non-template function in another translation unit?
>
I actually believe that the standard specifies that koenig lookup (which
applies in this case) will find the friend declaration, and since it is
a non template version it is a better viable function than the template
version and will be chosen over it.
The set of associated namespaces here is global, and set of associated
classes is 'A'.
So I think the function call should not cause an implicit instantiation
of the template specialization with A as the parametrized type.

regards,
-fais


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/07/19
Raw View
The new rules about friend declarations not really declaring a
function in the enclosing namespace, but only declaring that if
there is a function there, it is a friend.

template<class T> void f (T& x) {
    x.i = 6;
    }
class A {
    friend void f (A&); // non-template friend
    int i;
    };
void (*pf)(A&) = f; // #1
void g () {
    A a;
    f(a); // #2
    }

#1 Should this be an error because there is no real declaration of f?

#2 Should the template be instantiated because there is no declaration
for the non-template function available?  It would not be a friend
and would produce an error, but that is not the question.  Does the
friend non-declaration prevent the instantiation and allow finding
a non-template function in another translation unit?

John


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