
adam.butcher@selex-comms.com wrote:
Hi Niels,
I tried the above with gcc 4.4.0 and stlport. It chose std::swap (stlport's) via ADL and failed to compile due to 'invalid array assignment' as expected. It seems that the issue is with gcc 4.4.0's latest standard library rather than a broken ADL or other such detail. Looks like the latest gcc std::swap supports c-style array swapping.
You are right. bits/algorithmfwd.h contains template <typename _Tp, size_t _Nm> void swap(_Tp (&)[_Nm], _Tp (&)[_Nm]);
#---------- GCC 4.4.0 + STLport 5.1.3 ------------
g++ swap.cpp swap.cpp:19: instantiated from here ..../STLport-5.1.3-native/stlport/stl/_algobase.h:93: error: invalid array assignment
#---------- GCC 4.4.0 + libstdc++ 4.4.0 -----------
CPLUS_INCLUDE_PATH= g++ swap.cpp ../a.out # no exception is thrown -- it's picking std::swap
#---------- GCC 4.2.3 + libstdc++ 4.2.3 ------------
CPLUS_INCLUDE_PATH= g++-4.2.3 swap.cpp swap.cpp:19: instantiated from here .../include/c++/4.2.3/bits/stl_algobase.h:100: error: invalid array assignment
#---------- GCC 4.4.0 + libstdc++ 4.4.0 -----------
The following compiles fine with 4.4.0 std library. Switch to stlport headers and it fails to compile as expected.
CPLUS_INCLUDE_PATH= g++ -xc++ - << EOF #include <string> #include <algorithm>
int main() { std::string a[42], b[42]; std::swap(a, b); } EOF #----------------------------------------
Does the C++0x library allow for std::swap with c-style arrays? If so, maybe the issue is that g++ is implementing it without needing you to specify --std=c++0x. I guess it is not breaking any existing use of std::swap -- just making a missing case work (?)
Just to check that it actually is swapping I changed the body of the last snippet to:
std::string a[42] = { "a", }, b[42] = { "b", }; std::swap(a, b); return a[0][0] - 'a';
and it did indeed swap. The return code was 1, not 0 -- so the first string in 'a' after the swap began with char 'b'.
Hope this info's useful. Regards Adam
BR, Dmitry