//===----------------------------------------------------------------------===// // // 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 // // unique_ptr // Test unique_ptr converting move assignment #include #include #include #include "deleter_types.h" #include "unique_ptr_test_helper.h" template void testAssign(APtr& aptr, BPtr& bptr) { A* p = bptr.get(); assert(A::count == 2); aptr = std::move(bptr); assert(aptr.get() == p); assert(bptr.get() == 0); assert(A::count == 1); assert(B::count == 1); } template void checkDeleter(LHS& lhs, RHS& rhs, int LHSState, int RHSState) { assert(lhs.get_deleter().state() == LHSState); assert(rhs.get_deleter().state() == RHSState); } template struct NCConvertingDeleter { NCConvertingDeleter() = default; NCConvertingDeleter(NCConvertingDeleter const&) = delete; NCConvertingDeleter(NCConvertingDeleter&&) = default; template NCConvertingDeleter(NCConvertingDeleter&&) {} void operator()(T*) const {} }; template struct NCConvertingDeleter { NCConvertingDeleter() = default; NCConvertingDeleter(NCConvertingDeleter const&) = delete; NCConvertingDeleter(NCConvertingDeleter&&) = default; template NCConvertingDeleter(NCConvertingDeleter&&) {} void operator()(T*) const {} }; struct NCGenericDeleter { NCGenericDeleter() = default; NCGenericDeleter(NCGenericDeleter const&) = delete; NCGenericDeleter(NCGenericDeleter&&) = default; void operator()(void*) const {} }; void test_sfinae() { using DA = NCConvertingDeleter; // non-copyable deleters using DB = NCConvertingDeleter; using UA = std::unique_ptr; using UB = std::unique_ptr; using UAD = std::unique_ptr; using UBD = std::unique_ptr; { // cannot move from an lvalue static_assert(std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } { // cannot move if the deleter-types cannot convert static_assert(std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } { // cannot move-convert with reference deleters of different types using UA1 = std::unique_ptr; using UB1 = std::unique_ptr; static_assert(!std::is_assignable::value, ""); } { // cannot move-convert with reference deleters of different types using UA1 = std::unique_ptr; using UB1 = std::unique_ptr; static_assert(!std::is_assignable::value, ""); } { // cannot move-convert from unique_ptr using UA1 = std::unique_ptr; using UA2 = std::unique_ptr; using UB1 = std::unique_ptr; static_assert(!std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } { // cannot move-convert from unique_ptr using UA1 = std::unique_ptr; using UA2 = std::unique_ptr; using UB1 = std::unique_ptr; static_assert(!std::is_assignable::value, ""); static_assert(!std::is_assignable::value, ""); } } int main() { test_sfinae(); { std::unique_ptr bptr(new B); std::unique_ptr aptr(new A); testAssign(aptr, bptr); } assert(A::count == 0); assert(B::count == 0); { Deleter del(42); std::unique_ptr > bptr(new B, std::move(del)); std::unique_ptr > aptr(new A); testAssign(aptr, bptr); checkDeleter(aptr, bptr, 42, 0); } assert(A::count == 0); assert(B::count == 0); { CDeleter adel(6); CDeleter bdel(42); std::unique_ptr&> bptr(new B, bdel); std::unique_ptr&> aptr(new A, adel); testAssign(aptr, bptr); checkDeleter(aptr, bptr, 42, 42); } assert(A::count == 0); assert(B::count == 0); }