//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // template class tuple; // template // tuple& operator=(pair&& u); // UNSUPPORTED: c++03 #include #include #include #include #include "test_macros.h" struct B { int id_; explicit B(int i = 0) : id_(i) {} virtual ~B() {} }; struct D : B { explicit D(int i) : B(i) {} }; struct TrackMove { TrackMove() : value(0), moved_from(false) { } explicit TrackMove(int v) : value(v), moved_from(false) { } TrackMove(TrackMove const& other) : value(other.value), moved_from(false) { } TrackMove(TrackMove&& other) : value(other.value), moved_from(false) { other.moved_from = true; } TrackMove& operator=(TrackMove const& other) { value = other.value; moved_from = false; return *this; } TrackMove& operator=(TrackMove&& other) { value = other.value; moved_from = false; other.moved_from = true; return *this; } int value; bool moved_from; }; struct NonAssignable { NonAssignable& operator=(NonAssignable const&) = delete; NonAssignable& operator=(NonAssignable&&) = delete; }; struct MoveAssignable { MoveAssignable& operator=(MoveAssignable const&) = delete; MoveAssignable& operator=(MoveAssignable&&) = default; }; struct CopyAssignable { CopyAssignable& operator=(CopyAssignable const&) = default; CopyAssignable& operator=(CopyAssignable&&) = delete; }; struct NothrowMoveAssignable { NothrowMoveAssignable& operator=(NothrowMoveAssignable&&) noexcept { return *this; } }; struct PotentiallyThrowingMoveAssignable { PotentiallyThrowingMoveAssignable& operator=(PotentiallyThrowingMoveAssignable&&) { return *this; } }; int main(int, char**) { { typedef std::pair> T0; typedef std::tuple> T1; T0 t0(2, std::unique_ptr(new D(3))); T1 t1; t1 = std::move(t0); assert(std::get<0>(t1) == 2); assert(std::get<1>(t1)->id_ == 3); } { using T = std::tuple; using P = std::pair; static_assert(!std::is_assignable::value, ""); } { using T = std::tuple; using P = std::pair; static_assert(!std::is_assignable::value, ""); } { typedef std::tuple Tuple; typedef std::pair Pair; static_assert(std::is_nothrow_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } { typedef std::tuple Tuple; typedef std::pair Pair; static_assert(std::is_assignable::value, ""); static_assert(!std::is_nothrow_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } { // We assign through the reference and don't move out of the incoming ref, // so this doesn't work (but would if the type were CopyAssignable). { using T = std::tuple; using P = std::pair; static_assert(!std::is_assignable::value, ""); } // ... works if it's CopyAssignable { using T = std::tuple; using P = std::pair; static_assert(std::is_assignable::value, ""); } // For rvalue-references, we can move-assign if the type is MoveAssignable // or CopyAssignable (since in the worst case the move will decay into a copy). { using T1 = std::tuple; using P1 = std::pair; static_assert(std::is_assignable::value, ""); using T2 = std::tuple; using P2 = std::pair; static_assert(std::is_assignable::value, ""); } // In all cases, we can't move-assign if the types are not assignable, // since we assign through the reference. { using T1 = std::tuple; using P1 = std::pair; static_assert(!std::is_assignable::value, ""); using T2 = std::tuple; using P2 = std::pair; static_assert(!std::is_assignable::value, ""); } } { // Make sure that we don't incorrectly move out of the source's reference. using Dest = std::tuple; using Source = std::pair; TrackMove track{3}; Source src(track, 4); assert(!track.moved_from); Dest dst; dst = std::move(src); // here we should make a copy assert(!track.moved_from); assert(std::get<0>(dst).value == 3); } { // But we do move out of the source's reference if it's a rvalue ref using Dest = std::tuple; using Source = std::pair; TrackMove track{3}; Source src(std::move(track), 4); assert(!track.moved_from); // we just took a reference Dest dst; dst = std::move(src); assert(track.moved_from); assert(std::get<0>(dst).value == 3); } { // If the pair holds a value, then we move out of it too using Dest = std::tuple; using Source = std::pair; Source src(TrackMove{3}, 4); Dest dst; dst = std::move(src); assert(src.first.moved_from); assert(std::get<0>(dst).value == 3); } return 0; }