From: Marshall Clow Date: Tue, 6 Jan 2015 19:20:49 +0000 (+0000) Subject: Fix PR 22106; make std::swap work for multi-dimensional arrays. Thanks to Peter Gries... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d632356aa311d4c5e3ac5edb30b3cbe51aa69f48;p=platform%2Fupstream%2Fllvm.git Fix PR 22106; make std::swap work for multi-dimensional arrays. Thanks to Peter Griess for the report and suggested fix llvm-svn: 225285 --- diff --git a/libcxx/include/utility b/libcxx/include/utility index 6f324db..2cb1018 100644 --- a/libcxx/include/utility +++ b/libcxx/include/utility @@ -202,6 +202,10 @@ operator>=(const _Tp& __x, const _Tp& __y) // swap_ranges +// forward +template +void swap(_Tp (&__a)[_Np], _Tp (&__b)[_Np]) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value); + template inline _LIBCPP_INLINE_VISIBILITY _ForwardIterator2 diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp index 24fc47e..e12c69b 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp @@ -62,6 +62,53 @@ test1() #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +void test2() +{ + { + int src[2][2] = {{0, 1}, {2, 3}}; + decltype(src) dest = {{9, 8}, {7, 6}}; + + std::swap(src, dest); + + assert ( src[0][0] == 9 ); + assert ( src[0][1] == 8 ); + assert ( src[1][0] == 7 ); + assert ( src[1][1] == 6 ); + + assert ( dest[0][0] == 0 ); + assert ( dest[0][1] == 1 ); + assert ( dest[1][0] == 2 ); + assert ( dest[1][1] == 3 ); + } + + { + int src[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; + decltype(src) dest = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; + + std::swap(src, dest); + + assert ( src[0][0] == 9 ); + assert ( src[0][1] == 8 ); + assert ( src[0][2] == 7 ); + assert ( src[1][0] == 6 ); + assert ( src[1][1] == 5 ); + assert ( src[1][2] == 4 ); + assert ( src[2][0] == 3 ); + assert ( src[2][1] == 2 ); + assert ( src[2][2] == 1 ); + + assert ( dest[0][0] == 0 ); + assert ( dest[0][1] == 1 ); + assert ( dest[0][2] == 2 ); + assert ( dest[1][0] == 3 ); + assert ( dest[1][1] == 4 ); + assert ( dest[1][2] == 5 ); + assert ( dest[2][0] == 6 ); + assert ( dest[2][1] == 7 ); + assert ( dest[2][2] == 8 ); + } +} + int main() { test, forward_iterator >(); @@ -107,4 +154,6 @@ int main() test1*, std::unique_ptr*>(); #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + + test2(); }