Comment #2 on issue 195 by jmccaskey: common.h should not have "using namespace std;"
http://code.google.com/p/protobuf/issues/detail?id=195

Yes, although the using statement is inside your own namespace it appears that at least in Visual C++ 2005 the encapsulation is leaky and having used the namespace can affect other code in the global name space. Maybe this is a Visual C++ bug, it does seem to be violating scope rules for the using directive somehow. Even if it is
Visual C++ specific though, what if my application code wanted to use the
google::protobuf name space but not std? Having that also automatically cause using
of std still seems undesirable.

Below is a simple test program that reproduces the issue directly just by including the common.h header. You'll notice I define a swap function directly in the global namespace, and it ends up causing ambiguity with the std::swap function. If the
"using namespace std;" is removed from common.h this problem disappears.

The compilation errors will be:

1>Compiling...
1>test.cpp
1>c:\users\jmccaskey\documents\visual studio 2005\projects\test\test\test.cpp(21) :
error C2668: 'swap' : ambiguous call to overloaded function
1>        c:\users\jmccaskey\documents\visual studio
2005\projects\test\test\test.cpp(8): could be 'void swap<int>(T &,T &)'
1>        with
1>        [
1>            T=int
1>        ]
1> c:\program files (x86)\microsoft visual studio 8\vc\include\utility(16): or
'void std::swap<int>(_Ty &,_Ty &)'
1>        with
1>        [
1>            _Ty=int
1>        ]
1>        while trying to match the argument list '(int, int)'



// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "google/protobuf/stubs/common.h"


template< class T >
void swap( T &a, T &b )
{
        T tmp = a;
        a = b;
        b = tmp;
}


int _tmain(int argc, _TCHAR* argv[])
{
        int one = 1;
        int two = 2;

        swap( one, two );
        return 0;
}


--
You received this message because you are subscribed to the Google Groups "Protocol 
Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to