Topic: Question about argument-dependent-lookup for namespace-scope


Author: neverhoodboy <xinjie.wang@gmail.com>
Date: Sun, 4 Mar 2012 14:25:42 -0800 (PST)
Raw View
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);  // will this compile?
}


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]




Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?=<daniel.kruegler@googlemail.com>
Date: Tue, 6 Mar 2012 08:50:23 -0800 (PST)
Raw View
On 2012-03-04 23:25, neverhoodboy wrote:
>  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);  // will this compile?
>  }

IMO the call of funcb is supposed to be ill-formed. According to 3.4.2
p4 b2:

"Any namespace-scope friend functions or friend function templates
declared in associated classes are visible [..]"

but for a non-template class BBB, only BBB itself (and not AAA) is an
associated class.

HTH&  Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]