Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11204

Re: ADL fails for friend functions...

From Edek <edek.pienkowski@gmail.com>
Newsgroups comp.lang.c++
Subject Re: ADL fails for friend functions...
Date 2011-11-02 14:40 +0100
Organization ATMAN - ATM S.A.
Message-ID <j8rh64$8b6$1@node2.news.atman.pl> (permalink)
References <22b4a754-8c28-494a-ba95-bcbb7bccdc57@u24g2000pru.googlegroups.com>

Show all headers | View raw


On 11/02/2011 02:23 PM, Werner wrote:
> Hi All,
>
> Here follows a minimal example that fails to compile:
>
> namespace n1{
> class X1
> {
>    friend void join( X1&, X1&  ){ }
> };

You need to bring join out to make ADL consider it

namespace n1{
class X1
{
   friend void join( X1&, X1& );
};

void join (X1&, X1&) {};
}
... and ...

>
> }
> class Y
> {
>    void join(){}
>    void foo()
>    {
>      struct{ void operator()( n1::X1&, n1::X1&  ){ } }join_x;
>      n1::X1 xa, xb;
>      join( xa, xb );
>    }
> };

class Y
{
   void join(){}
   void foo()
   {
     struct{ void operator()( n1::X1&, n1::X1& ){ } }join_x;
     n1::X1 xa, xb;

     using ::n1::join;	
     join( xa, xb );
   }
};

Local scope has preference in the standard: if there is a local
symbol, other lookup methods are not attempted, unless you bring
it in with "using". (I am using non-formal language, see
the standard on "using" directive). Only then regular overload
matching kicks in.

>
> The friend function join is not found because ADL is
> "negated" by the Y::join.
>
> Is there any other way to call join defined in X from within foo?
> Apart
> from moving the call outside the scope of Y (see next example)?

The above compiles with gcc and is consistent with what I know.

> The reason I'm asking this question is because of Andrei
> Alexandrescu's
> choice in Modern C++ Design to use friend functions instead of member
> functions for reset (compared to boost that does the opposite). I
> realize
> his rationale is/was to prevent accidental calling of reset for the
> contained
> type...

Could you quote directly, I am confused with what you wrote, it does not
fit anything I can come up with.

Edek

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ADL fails for friend functions... Werner <werasm@gmail.com> - 2011-11-02 06:23 -0700
  Re: ADL fails for friend functions... Edek <edek.pienkowski@gmail.com> - 2011-11-02 14:40 +0100
    Re: ADL fails for friend functions... Werner <werasm@gmail.com> - 2011-11-02 07:28 -0700
      Re: ADL fails for friend functions... Edek <edek.pienkowski@gmail.com> - 2011-11-02 16:08 +0100
      Re: ADL fails for friend functions... Victor Bazarov <v.bazarov@comcast.invalid> - 2011-11-02 12:34 -0400

csiph-web