Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <memory> |
| 11 | |
| 12 | // template <class Y, class D> explicit shared_ptr(unique_ptr<Y, D>&&r); |
| 13 | |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 14 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 72aab5f | 2014-11-04 05:11:41 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <new> |
| 18 | #include <cstdlib> |
| 19 | #include <cassert> |
| 20 | |
| 21 | bool throw_next = false; |
| 22 | |
| 23 | void* operator new(std::size_t s) throw(std::bad_alloc) |
| 24 | { |
| 25 | if (throw_next) |
| 26 | throw std::bad_alloc(); |
| 27 | return std::malloc(s); |
| 28 | } |
| 29 | |
| 30 | void operator delete(void* p) throw() |
| 31 | { |
| 32 | std::free(p); |
| 33 | } |
| 34 | |
| 35 | struct B |
| 36 | { |
| 37 | static int count; |
| 38 | |
| 39 | B() {++count;} |
| 40 | B(const B&) {++count;} |
| 41 | virtual ~B() {--count;} |
| 42 | }; |
| 43 | |
| 44 | int B::count = 0; |
| 45 | |
| 46 | struct A |
| 47 | : public B |
| 48 | { |
| 49 | static int count; |
| 50 | |
| 51 | A() {++count;} |
| 52 | A(const A&) {++count;} |
| 53 | ~A() {--count;} |
| 54 | }; |
| 55 | |
| 56 | int A::count = 0; |
| 57 | |
Marshall Clow | 7683fe2 | 2014-11-18 18:14:53 +0000 | [diff] [blame] | 58 | void fn ( const std::shared_ptr<int> &) {} |
| 59 | void fn ( const std::shared_ptr<B> &) { assert (false); } |
| 60 | |
Marshall Clow | 0ad232a | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 61 | template <typename T> |
| 62 | void assert_deleter ( T * ) { assert(false); } |
| 63 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 64 | int main() |
| 65 | { |
| 66 | { |
| 67 | std::unique_ptr<A> ptr(new A); |
| 68 | A* raw_ptr = ptr.get(); |
| 69 | std::shared_ptr<B> p(std::move(ptr)); |
| 70 | assert(A::count == 1); |
| 71 | assert(B::count == 1); |
| 72 | assert(p.use_count() == 1); |
| 73 | assert(p.get() == raw_ptr); |
| 74 | assert(ptr.get() == 0); |
| 75 | } |
| 76 | assert(A::count == 0); |
| 77 | { |
| 78 | std::unique_ptr<A> ptr(new A); |
| 79 | A* raw_ptr = ptr.get(); |
| 80 | throw_next = true; |
| 81 | try |
| 82 | { |
| 83 | std::shared_ptr<B> p(std::move(ptr)); |
| 84 | assert(false); |
| 85 | } |
| 86 | catch (...) |
| 87 | { |
Sean Hunt | 541cb30 | 2011-07-18 23:51:25 +0000 | [diff] [blame] | 88 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 89 | assert(A::count == 1); |
| 90 | assert(B::count == 1); |
| 91 | assert(ptr.get() == raw_ptr); |
Sean Hunt | 541cb30 | 2011-07-18 23:51:25 +0000 | [diff] [blame] | 92 | #else |
| 93 | assert(A::count == 0); |
| 94 | assert(B::count == 0); |
| 95 | assert(ptr.get() == 0); |
| 96 | #endif |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | assert(A::count == 0); |
Marshall Clow | 7683fe2 | 2014-11-18 18:14:53 +0000 | [diff] [blame] | 100 | |
| 101 | // LWG 2399 |
| 102 | { |
| 103 | throw_next = false; |
| 104 | fn(std::unique_ptr<int>(new int)); |
| 105 | } |
Marshall Clow | 0ad232a | 2015-05-10 13:59:45 +0000 | [diff] [blame] | 106 | |
| 107 | #if __cplusplus >= 201402L |
| 108 | // LWG 2415 |
| 109 | { |
| 110 | std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>); |
| 111 | std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope |
| 112 | } |
| 113 | #endif |
| 114 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 115 | } |