| 1 | // { dg-do compile { target c++11 } } |
| 2 | |
| 3 | // Copyright (C) 2012-2025 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // You should have received a copy of the GNU General Public License along |
| 17 | // with this library; see the file COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | // 20.7.1 Class template unique_ptr [unique.ptr] |
| 21 | |
| 22 | #include <memory> |
| 23 | |
| 24 | struct A { virtual ~A() = default; }; |
| 25 | |
| 26 | struct B : A { }; |
| 27 | |
| 28 | // Construction from objects with different cv-qualification |
| 29 | |
| 30 | void |
| 31 | test01() |
| 32 | { |
| 33 | std::unique_ptr<const A> cA(new A); |
| 34 | std::unique_ptr<volatile A> vA(new A); |
| 35 | std::unique_ptr<const volatile A> cvA(new A); |
| 36 | } |
| 37 | |
| 38 | void |
| 39 | test02() |
| 40 | { |
| 41 | std::unique_ptr<const A> cB(new B); |
| 42 | std::unique_ptr<volatile A> vB(new B); |
| 43 | std::unique_ptr<const volatile A> cvB(new B); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | test03() |
| 48 | { |
| 49 | std::unique_ptr<A> upA; |
| 50 | |
| 51 | std::unique_ptr<const A> cA(std::move(upA)); |
| 52 | std::unique_ptr<volatile A> vA(std::move(upA)); |
| 53 | std::unique_ptr<const volatile A> cvA(std::move(upA)); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | test04() |
| 58 | { |
| 59 | std::unique_ptr<B> upB; |
| 60 | |
| 61 | std::unique_ptr<const A> cA(std::move(upB)); |
| 62 | std::unique_ptr<volatile A> vA(std::move(upB)); |
| 63 | std::unique_ptr<const volatile A> cvA(std::move(upB)); |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | test05() |
| 68 | { |
| 69 | std::unique_ptr<const A[]> cA(new A[1]); |
| 70 | std::unique_ptr<volatile A[]> vA(new A[1]); |
| 71 | std::unique_ptr<const volatile A[]> cvA(new A[1]); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | test06() |
| 76 | { |
| 77 | std::unique_ptr<A[]> upA; |
| 78 | |
| 79 | std::unique_ptr<const A[]> cA(std::move(upA)); |
| 80 | std::unique_ptr<volatile A[]> vA(std::move(upA)); |
| 81 | std::unique_ptr<const volatile A[]> cvA(std::move(upA)); |
| 82 | } |
| 83 | |
| 84 | struct A_pointer { operator A*() const { return nullptr; } }; |
| 85 | |
| 86 | void |
| 87 | test07() |
| 88 | { |
| 89 | // Allow conversions from user-defined pointer-like types |
| 90 | // for the single-object version |
| 91 | A_pointer p; |
| 92 | std::unique_ptr<A> upA(p); |
| 93 | std::unique_ptr<const A> cA(p); |
| 94 | std::unique_ptr<volatile A> vA(p); |
| 95 | std::unique_ptr<const volatile A> cvA(p); |
| 96 | // Allow conversions from user-defined pointer-like types |
| 97 | // for the array version when the type is converted explicitly |
| 98 | std::unique_ptr<A[]> upA2((A*)p); |
| 99 | std::unique_ptr<const A[]> cA2((A*)p); |
| 100 | std::unique_ptr<volatile A[]> vA2((A*)p); |
| 101 | std::unique_ptr<const volatile A[]> cvA2((A*)p); |
| 102 | } |