//===----------------------------------------------------------------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // unique_ptr // Test unique_ptr move assignment // test move assignment. Should only require a MoveConstructible deleter, or if // deleter is a reference, not even that. #include #include #include "../../deleter.h" struct A { static int count; A() {++count;} A(const A&) {++count;} ~A() {--count;} }; int A::count = 0; int main() { { std::unique_ptr s1(new A); A* p = s1.get(); std::unique_ptr s2(new A); assert(A::count == 2); s2 = std::move(s1); assert(A::count == 1); assert(s2.get() == p); assert(s1.get() == 0); } assert(A::count == 0); { std::unique_ptr > s1(new A, Deleter(5)); A* p = s1.get(); std::unique_ptr > s2(new A); assert(A::count == 2); s2 = std::move(s1); assert(s2.get() == p); assert(s1.get() == 0); assert(A::count == 1); assert(s2.get_deleter().state() == 5); assert(s1.get_deleter().state() == 0); } assert(A::count == 0); { CDeleter d1(5); std::unique_ptr&> s1(new A, d1); A* p = s1.get(); CDeleter d2(6); std::unique_ptr&> s2(new A, d2); s2 = std::move(s1); assert(s2.get() == p); assert(s1.get() == 0); assert(A::count == 1); assert(d1.state() == 5); assert(d2.state() == 5); } assert(A::count == 0); }