From aedcbf898b7136b3aa09f236240eb5f1f05e4b78 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Mon, 25 Jul 2016 02:36:42 +0000 Subject: [PATCH] Recommit r276548 - Make pair/tuples assignment operators SFINAE properly. I think I've solved issues with is_assignable and references to incomplete types. The updated patch adds tests for this case. llvm-svn: 276603 --- libcxx/include/tuple | 22 ++++ libcxx/include/utility | 42 +++++--- .../tuple.tuple/tuple.assign/copy.pass.cpp | 49 +++++++++ .../tuple.tuple/tuple.assign/move.pass.cpp | 69 ++++++++++++ .../pairs/pairs.pair/assign_pair.pass.cpp | 101 ++++++++++++++++++ .../pairs.pair/assign_pair_cxx03.pass.cpp | 49 +++++++++ .../pairs/pairs.pair/assign_rv_pair.pass.cpp | 68 +++++++++++- 7 files changed, 386 insertions(+), 14 deletions(-) create mode 100644 libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp create mode 100644 libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp diff --git a/libcxx/include/tuple b/libcxx/include/tuple index 31613d2b94f0..a68f115b68c1 100644 --- a/libcxx/include/tuple +++ b/libcxx/include/tuple @@ -647,6 +647,9 @@ public: _LIBCPP_CONSTEXPR tuple() _NOEXCEPT_(__all::value...>::value) {} + tuple(tuple const&) = default; + tuple(tuple&&) = default; + template , @@ -885,6 +888,25 @@ public: tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t) : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {} + using _CanCopyAssign = __all::value...>; + using _CanMoveAssign = __all::value...>; + + _LIBCPP_INLINE_VISIBILITY + tuple& operator=(typename conditional<_CanCopyAssign::value, tuple, __nat>::type const& __t) + _NOEXCEPT_((__all::value...>::value)) + { + base_.operator=(__t.base_); + return *this; + } + + _LIBCPP_INLINE_VISIBILITY + tuple& operator=(typename conditional<_CanMoveAssign::value, tuple, __nat>::type&& __t) + _NOEXCEPT_((__all::value...>::value)) + { + base_.operator=(static_cast(__t.base_)); + return *this; + } + template ::value + && is_copy_assignable<_T2>::value, + pair, __nat + >::type _CopyAssignT; + typedef typename conditional< + is_move_assignable<_T1>::value + && is_move_assignable<_T2>::value, + pair, __nat + >::type _MoveAssignT; +#else + typedef pair _CopyAssignT; +#endif + _LIBCPP_INLINE_VISIBILITY - pair& operator=(const pair& __p) + pair& operator=(_CopyAssignT const& __p) _NOEXCEPT_(is_nothrow_copy_assignable::value && is_nothrow_copy_assignable::value) { @@ -358,6 +373,18 @@ struct _LIBCPP_TYPE_VIS_ONLY pair return *this; } +#ifndef _LIBCPP_CXX03_LANG + _LIBCPP_INLINE_VISIBILITY + pair& operator=(_MoveAssignT&& __p) + _NOEXCEPT_(is_nothrow_move_assignable::value && + is_nothrow_move_assignable::value) + { + first = _VSTD::forward(__p.first); + second = _VSTD::forward(__p.second); + return *this; + } +#endif + #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES template (__p.first)), second(_VSTD::forward<_U2>(__p.second)) {} - _LIBCPP_INLINE_VISIBILITY - pair& - operator=(pair&& __p) _NOEXCEPT_(is_nothrow_move_assignable::value && - is_nothrow_move_assignable::value) - { - first = _VSTD::forward(__p.first); - second = _VSTD::forward(__p.second); - return *this; - } + #ifndef _LIBCPP_HAS_NO_VARIADICS - template::value>::type> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 @@ -411,7 +429,7 @@ struct _LIBCPP_TYPE_VIS_ONLY pair {} template ::value>::type> + class = typename enable_if::type, pair>::value && __tuple_assignable<_Tuple, pair>::value>::type> _LIBCPP_INLINE_VISIBILITY pair& operator=(_Tuple&& __p) diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/copy.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/copy.pass.cpp index d5d020779726..6a24c278e1a1 100644 --- a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/copy.pass.cpp +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/copy.pass.cpp @@ -19,6 +19,22 @@ #include #include +#include "test_macros.h" + +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable &&) = delete; +}; +static_assert(std::is_copy_assignable::value, ""); +struct MoveAssignable { + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + int main() { { @@ -51,4 +67,37 @@ int main() assert(std::get<1>(t) == 'a'); assert(std::get<2>(t) == "some text"); } + { + // test reference assignment. + using T = std::tuple; + int x = 42; + int y = 100; + int x2 = -1; + int y2 = 500; + T t(x, std::move(y)); + T t2(x2, std::move(y2)); + t = t2; + assert(std::get<0>(t) == x2); + assert(&std::get<0>(t) == &x); + assert(std::get<1>(t) == y2); + assert(&std::get<1>(t) == &y); + } + { + // test that the implicitly generated copy assignment operator + // is properly deleted + using T = std::tuple>; + static_assert(!std::is_copy_assignable::value, ""); + } + { + using T = std::tuple; + static_assert(!std::is_copy_assignable::value, ""); + } + { + using T = std::tuple; + static_assert(std::is_copy_assignable::value, ""); + } + { + using T = std::tuple; + static_assert(!std::is_copy_assignable::value, ""); + } } diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/move.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/move.pass.cpp index fc5e41ad569d..210f14be318a 100644 --- a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/move.pass.cpp +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/move.pass.cpp @@ -21,6 +21,33 @@ #include "MoveOnly.h" +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable&&) = delete; +}; +static_assert(std::is_copy_assignable::value, ""); +struct MoveAssignable { + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + + +struct CountAssign { + static int copied; + static int moved; + static void reset() { copied = moved = 0; } + CountAssign() = default; + CountAssign& operator=(CountAssign const&) { ++copied; return *this; } + CountAssign& operator=(CountAssign&&) { ++moved; return *this; } +}; +int CountAssign::copied = 0; +int CountAssign::moved = 0; + + int main() { { @@ -53,4 +80,46 @@ int main() assert(std::get<1>(t) == 1); assert(std::get<2>(t) == 2); } + { + // test reference assignment. + using T = std::tuple; + int x = 42; + int y = 100; + int x2 = -1; + int y2 = 500; + T t(x, std::move(y)); + T t2(x2, std::move(y2)); + t = std::move(t2); + assert(std::get<0>(t) == x2); + assert(&std::get<0>(t) == &x); + assert(std::get<1>(t) == y2); + assert(&std::get<1>(t) == &y); + } + { + // test that the implicitly generated move assignment operator + // is properly deleted + using T = std::tuple>; + static_assert(std::is_move_assignable::value, ""); + static_assert(!std::is_copy_assignable::value, ""); + + } + { + using T = std::tuple; + static_assert(!std::is_move_assignable::value, ""); + } + { + using T = std::tuple; + static_assert(std::is_move_assignable::value, ""); + } + { + // The move should decay to a copy. + CountAssign::reset(); + using T = std::tuple; + static_assert(std::is_move_assignable::value, ""); + T t1; + T t2; + t1 = std::move(t2); + assert(CountAssign::copied == 1); + assert(CountAssign::moved == 0); + } } diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp new file mode 100644 index 000000000000..3f7066310002 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair.pass.cpp @@ -0,0 +1,101 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++98, c++03 + +// + +// template struct pair + +// pair& operator=(pair const& p); + +#include +#include +#include + + +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable() = default; + CopyAssignable(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable&&) = delete; +}; +struct MoveAssignable { + MoveAssignable() = default; + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + +struct CountAssign { + static int copied; + static int moved; + static void reset() { copied = moved = 0; } + CountAssign() = default; + CountAssign& operator=(CountAssign const&) { ++copied; return *this; } + CountAssign& operator=(CountAssign&&) { ++moved; return *this; } +}; +int CountAssign::copied = 0; +int CountAssign::moved = 0; + +struct Incomplete; +extern Incomplete inc_obj; + +int main() +{ + { + typedef std::pair P; + const P p1(CopyAssignable(), 4); + P p2; + p2 = p1; + assert(p2.second == 4); + } + { + using P = std::pair; + int x = 42; + int y = 101; + int x2 = -1; + int y2 = 300; + P p1(x, std::move(y)); + P p2(x2, std::move(y2)); + p1 = p2; + assert(p1.first == x2); + assert(p1.second == y2); + } + { + using P = std::pair; + static_assert(!std::is_copy_assignable

::value, ""); + } + { + CountAssign::reset(); + using P = std::pair; + static_assert(std::is_copy_assignable

::value, ""); + P p; + P p2; + p = p2; + assert(CountAssign::copied == 1); + assert(CountAssign::moved == 0); + } + { + using P = std::pair; + static_assert(!std::is_copy_assignable

::value, ""); + } + { + using P = std::pair; + static_assert(!std::is_copy_assignable

::value, ""); + P p(42, inc_obj); + assert(&p.second == &inc_obj); + } +} + +struct Incomplete {}; +Incomplete inc_obj; diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp new file mode 100644 index 000000000000..2623b800fff7 --- /dev/null +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_pair_cxx03.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// REQUIRES-ANY: c++98, c++03 + +// + +// template struct pair + +// pair& operator=(pair const& p); + +#include +#include +#include + +struct NonAssignable { + NonAssignable() {} +private: + NonAssignable& operator=(NonAssignable const&); +}; + +struct Incomplete; +extern Incomplete inc_obj; + +int main() +{ + { + // Test that we don't constrain the assignment operator in C++03 mode. + // Since we don't have access control SFINAE having pair evaluate SFINAE + // may cause a hard error. + typedef std::pair P; + static_assert(std::is_copy_assignable

::value, ""); + } + { + typedef std::pair P; + static_assert(std::is_copy_assignable

::value, ""); + P p(42, inc_obj); + assert(&p.second == &inc_obj); + } +} + +struct Incomplete {}; +Incomplete inc_obj; diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp index a753ee520dfa..38089200e4da 100644 --- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp +++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/assign_rv_pair.pass.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // // template struct pair @@ -17,9 +19,35 @@ #include #include + +struct NonAssignable { + NonAssignable& operator=(NonAssignable const&) = delete; + NonAssignable& operator=(NonAssignable&&) = delete; +}; +struct CopyAssignable { + CopyAssignable() = default; + CopyAssignable& operator=(CopyAssignable const&) = default; + CopyAssignable& operator=(CopyAssignable&&) = delete; +}; +struct MoveAssignable { + MoveAssignable() = default; + MoveAssignable& operator=(MoveAssignable const&) = delete; + MoveAssignable& operator=(MoveAssignable&&) = default; +}; + +struct CountAssign { + static int copied; + static int moved; + static void reset() { copied = moved = 0; } + CountAssign() = default; + CountAssign& operator=(CountAssign const&) { ++copied; return *this; } + CountAssign& operator=(CountAssign&&) { ++moved; return *this; } +}; +int CountAssign::copied = 0; +int CountAssign::moved = 0; + int main() { -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::pair, short> P; P p1(std::unique_ptr(new int(3)), 4); @@ -28,5 +56,41 @@ int main() assert(*p2.first == 3); assert(p2.second == 4); } -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES + { + using P = std::pair; + int x = 42; + int y = 101; + int x2 = -1; + int y2 = 300; + P p1(x, std::move(y)); + P p2(x2, std::move(y2)); + p1 = std::move(p2); + assert(p1.first == x2); + assert(p1.second == y2); + } + { + using P = std::pair; + static_assert(!std::is_move_assignable

::value, ""); + } + { + // The move decays to the copy constructor + CountAssign::reset(); + using P = std::pair; + static_assert(std::is_move_assignable

::value, ""); + P p; + P p2; + p = std::move(p2); + assert(CountAssign::moved == 0); + assert(CountAssign::copied == 1); + } + { + CountAssign::reset(); + using P = std::pair; + static_assert(std::is_move_assignable

::value, ""); + P p; + P p2; + p = std::move(p2); + assert(CountAssign::moved == 1); + assert(CountAssign::copied == 0); + } }