Re: Non-conforming g++ and msvc for inline friend func? Helpappreciated.

Discussion in 'C++' started by Öö Tiib, May 4, 2010.

  1. Öö Tiib

    Öö Tiib Guest

    On May 4, 4:25 am, "Alf P. Steinbach" <> wrote:
    > Comeau compiles fine, g++ and msvc fail.
    >
    > <code file="x.cpp">
    > #include <algorithm>
    >
    > namespace adl_is_bad {
    >
    >      struct Foo
    >      {
    >          friend void swap( Foo&, Foo& ) {}
    >      };
    >
    >      struct Bar
    >      {
    >          Foo     myFoo;
    >          int     myInt;
    >
    >          Bar(): myInt( 0 ) {}
    >
    >          friend void swap( Bar& a, Bar& b )
    >          {
    >              using namespace std;
    >              //sswap( a.myFoo, b.myFoo );
    >              swap( a.myInt, b.myInt );
    >          }
    >      };
    >
    > }  // namespace adl_is_bad
    >
    > int main() {}
    > </code>
    >
    > <comeau>
    > Your Comeau C/C++ test results are as follows:
    >
    > Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    > Copyright 1988-2008 Comeau Computing.  All rights reserved.
    > MODE:strict errors C++ C++0x_extensions
    >
    > In strict mode, with -tused, Compile succeeded (but remember, the Comeau online
    > compiler does not link).
    > Compiled with C++0x extensions enabled.
    > </comeau>
    >
    > <g++ 4.4.1>
    > C:\test> gnuc --version
    > g++ (TDM-2 mingw32) 4.4.1
    > Copyright (C) 2009 Free Software Foundation, Inc.
    > This is free software; see the source for copying conditions.  There is NO
    > warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    >
    > C:\test> gnuc x.cpp
    > x.cpp: In function 'void adl_is_bad::swap(adl_is_bad::Bar&, adl_is_bad::Bar&)':
    > x.cpp:21: error: 'swap' was not declared in this scope
    >
    > C:\test> _
    > </g++ 4.4.1>
    >
    > <msvc 7.1>
    > C:\test> msvc --version
    > Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.6030 for 80x86
    > Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.
    >
    > usage: cl [ option... ] filename... [ /link linkoption... ]
    >
    > C:\test> msvc x.cpp
    > x.cpp
    > x.cpp(21) : error C3767: 'swap' matching function is not accessible
    >          could be the friend function at 'x.cpp(7)' : 'swap'  [may be found via
    > argument-dependent lookup]
    >          or the friend function at       'x.cpp(17)' : 'swap'  [may be found via
    > argument-dependent lookup]
    >
    > C:\test> _
    > </msvc 7.1>
    >
    > <msvc 9.0>
    > C:\test> msvc --version
    > Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
    > Copyright (C) Microsoft Corporation.  All rights reserved.
    >
    > usage: cl [ option... ] filename... [ /link linkoption... ]
    >
    > C:\test> msvc x.cpp
    > x.cpp
    > x.cpp(21) : error C2664: 'void adl_is_bad::swap(adl_is_bad::Bar
    > &,adl_is_bad::Bar &)' : cannot convert parameter 1 from
    > 'int' to 'adl_is_bad::Bar &'
    >
    > C:\test> _
    > </msvc 9.0>
    >
    > Which compiler is right, if any?


    Probably it has something to do with the (3.4.3.2) and the like that
    make at least my head hurt. Whoever is right does not matter since
    that code is not portable anyway in practice like we see?


    >
    > Is there a commonly used solution for this?
    >
    > Or perhaps, just a solution?


    That name lookup has always been a puzzle. Compilers have always got
    it with slight bugs and differences. Currently their results are lot
    closer to each other than few years ago. I still avoid clashing names
    with names in other namespaces. When i have to do it then i use fully
    qualified names when calling to keep it easy to read and to understand
    (for both me and computer).

    If to replace "using namespace std" with "using std::swap" then it
    feels clear that it should compile. If to call std::swap() and no
    "using" stuff at all then it also should work. Your example hits
    exactly the spot for what i am too dumb lawyer to understand how it
    should be correct.
    Öö Tiib, May 4, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Senthilraja
    Replies:
    7
    Views:
    575
    stelios xanthakis
    Oct 15, 2003
  2. Johnny
    Replies:
    3
    Views:
    455
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    504
    Hari Sekhon
    Jun 20, 2006
  4. Alex Vinokur
    Replies:
    6
    Views:
    356
    Tor Rustad
    Nov 18, 2006
  5. Alf P. Steinbach
    Replies:
    7
    Views:
    609
    Paul Bibbings
    May 4, 2010
Loading...

Share This Page