Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Non-conforming g++ and msvc for inline friend func? Helpappreciated.

Reply
Thread Tools

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

 
 
Öö Tiib
Guest
Posts: n/a
 
      05-04-2010
On May 4, 4:25*am, "Alf P. Steinbach" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-conforming g++ and msvc for inline friend func? Help appreciated. Alf P. Steinbach C++ 7 05-04-2010 08:41 PM
Friend declaration not accepted by MSVC Alf P. Steinbach C++ 5 08-12-2009 04:54 PM
[MSVC++]Inline Assembly Galbu C++ 1 08-28-2005 11:07 PM



Advertisments