How does Argument-Dependent-Lookup work for namespace-scope friend fucntion?

N

neverhoodboy

We know that argument-dependent-lookup can be used to find namespace-
scope friend function defined in some class which is otherwise not
visible using an ordinary lookup. My question is, can the argument be
of some class in the same namespace other the enclosing class of the
friend function? Please see the following code snippet:

namespace NNN
{
class BBB {};
class AAA
{
public:
friend void func() {}
friend void funca(AAA&) {}
friend void funcb(BBB&) {}
};
}

int main()
{
NNN::AAA a;
NNN::BBB b;
NNN::func(); // this will fail to compile for sure
funca(a); // this will compile with no problem because of ADL
funcb(b); // shall this compile? various compilers at my hand
give different results, so I'm asking for help to understand the exact
requirement of the C++ standards.
}
 
J

Johannes Schaub

Am 04.03.2012 12:31, schrieb neverhoodboy:
We know that argument-dependent-lookup can be used to find namespace-
scope friend function defined in some class which is otherwise not
visible using an ordinary lookup. My question is, can the argument be
of some class in the same namespace other the enclosing class of the
friend function? Please see the following code snippet:


No it cannot, unless the other class brings the first class in
consideration by making it an associated class (because, for example,
the first class is a base class of the other class).

That you get varying results from different compilers is because the C++
specification was unclear in the past and was updated by defect reports
to be clear that unassociated classes do not make their friend function
declarations visible to ADL.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top