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