1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // We voluntarily use std::default_initializable on types that have redundant
12 // or ignored cv-qualifiers -- don't warn about it.
13 // ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-ignored-qualifiers
14 
15 // template<class T>
16 //     concept default_initializable = constructible_from<T> &&
17 //     requires { T{}; } &&
18 //     is-default-initializable<T>;
19 
20 #include <array>
21 #include <concepts>
22 #include <deque>
23 #include <forward_list>
24 #include <list>
25 #include <map>
26 #include <memory>
27 #include <queue>
28 #include <set>
29 #include <span>
30 #include <stack>
31 #include <string>
32 #include <string_view>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <vector>
36 
37 #include "test_macros.h"
38 
39 struct Empty {};
40 
41 struct CtorDefaulted {
42   CtorDefaulted() = default;
43 };
44 struct CtorDeleted {
45   CtorDeleted() = delete;
46 };
47 struct DtorDefaulted {
48   ~DtorDefaulted() = default;
49 };
50 struct DtorDeleted {
51   ~DtorDeleted() = delete;
52 };
53 
54 struct Noexcept {
55   ~Noexcept() noexcept;
56 };
57 struct NoexceptTrue {
58   ~NoexceptTrue() noexcept(true);
59 };
60 struct NoexceptFalse {
61   ~NoexceptFalse() noexcept(false);
62 };
63 
64 struct CtorProtected {
65 protected:
66   CtorProtected() = default;
67 };
68 struct CtorPrivate {
69 private:
70   CtorPrivate() = default;
71 };
72 struct DtorProtected {
73 protected:
74   ~DtorProtected() = default;
75 };
76 struct DtorPrivate {
77 private:
78   ~DtorPrivate() = default;
79 };
80 
81 template <class T>
82 struct NoexceptDependant {
83   ~NoexceptDependant() noexcept(std::is_same_v<T, int>);
84 };
85 
86 struct CtorExplicit {
87   explicit CtorExplicit() = default;
88 };
89 struct CtorArgument {
CtorArgumentCtorArgument90   CtorArgument(int) {}
91 };
92 struct CtorDefaultArgument {
CtorDefaultArgumentCtorDefaultArgument93   CtorDefaultArgument(int = 0) {}
94 };
95 struct CtorExplicitDefaultArgument {
CtorExplicitDefaultArgumentCtorExplicitDefaultArgument96   explicit CtorExplicitDefaultArgument(int = 0) {}
97 };
98 
99 struct Derived : public Empty {};
100 
101 class Abstract {
102   virtual void foo() = 0;
103 };
104 
105 class AbstractDestructor {
106   virtual ~AbstractDestructor() = 0;
107 };
108 
109 class OperatorNewDeleted {
110   void* operator new(std::size_t) = delete;
111   void operator delete(void* ptr) = delete;
112 };
113 
__anon017509f20102(const int&, int&&, double)114 [[maybe_unused]] auto Lambda = [](const int&, int&&, double){};
115 
116 template<class T>
test_not_const()117 void test_not_const()
118 {
119     static_assert( std::default_initializable<               T>);
120     static_assert(!std::default_initializable<const          T>);
121     static_assert( std::default_initializable<      volatile T>);
122     static_assert(!std::default_initializable<const volatile T>);
123 }
124 
125 template<class T>
test_true()126 void test_true()
127 {
128     static_assert( std::default_initializable<               T>);
129     static_assert( std::default_initializable<const          T>);
130     static_assert( std::default_initializable<      volatile T>);
131     static_assert( std::default_initializable<const volatile T>);
132 }
133 
134 template<class T>
test_false()135 void test_false()
136 {
137     static_assert(!std::default_initializable<               T>);
138     static_assert(!std::default_initializable<const          T>);
139     static_assert(!std::default_initializable<      volatile T>);
140     static_assert(!std::default_initializable<const volatile T>);
141 }
142 
test()143 void test()
144 {
145     test_not_const<bool>();
146     test_not_const<char>();
147     test_not_const<int>();
148     test_not_const<double>();
149 
150     test_false    <void>();
151     test_not_const<void*>();
152 
153     test_not_const<int*>();
154     test_false    <int[]>();
155     test_not_const<int[1]>();
156     test_false    <int&>();
157     test_false    <int&&>();
158 
159     test_true     <Empty>();
160 
161     test_true     <CtorDefaulted>();
162     test_false    <CtorDeleted>();
163     test_true     <DtorDefaulted>();
164     test_false    <DtorDeleted>();
165 
166     test_true     <Noexcept>();
167     test_true     <NoexceptTrue>();
168     test_false    <NoexceptFalse>();
169 
170     test_false    <CtorProtected>();
171     test_false    <CtorPrivate>();
172     test_false    <DtorProtected>();
173     test_false    <DtorPrivate>();
174 
175     test_true     <NoexceptDependant<int>>();
176     test_false    <NoexceptDependant<double>>();
177 
178     test_true     <CtorExplicit>();
179     test_false    <CtorArgument>();
180     test_true     <CtorDefaultArgument>();
181     test_true     <CtorExplicitDefaultArgument>();
182 
183     test_true     <Derived>();
184     test_false    <Abstract>();
185     test_false    <AbstractDestructor>();
186 
187     test_true     <OperatorNewDeleted>();
188 
189     test_true     <decltype(Lambda)>();
190     test_not_const<void(*)(const int&)>();
191     test_not_const<void(Empty::*)(const int&)               >();
192     test_not_const<void(Empty::*)(const int&) const         >();
193     test_not_const<void(Empty::*)(const int&)       volatile>();
194     test_not_const<void(Empty::*)(const int&) const volatile>();
195     test_not_const<void(Empty::*)(const int&) &>();
196     test_not_const<void(Empty::*)(const int&) &&>();
197     test_not_const<void(Empty::*)(const int&) noexcept>();
198     test_not_const<void(Empty::*)(const int&) noexcept(true)>();
199     test_not_const<void(Empty::*)(const int&) noexcept(false)>();
200 
201     // Sequence containers
202     test_true     <std::array<               int, 0>>();
203     test_not_const<std::array<               int, 1>>();
204     test_false    <std::array<const          int, 1>>();
205     test_not_const<std::array<      volatile int, 1>>();
206     test_false    <std::array<const volatile int, 1>>();
207     test_true     <std::deque<               int>>();
208     test_true     <std::forward_list<int>>();
209     test_true     <std::list<int>>();
210     test_true     <std::vector<int>>();
211 
212     // Associative containers
213     test_true     <std::set<int>>();
214     test_true     <std::map<int, int>>();
215     test_true     <std::multiset<int>>();
216     test_true     <std::multimap<int, int>>();
217 
218     // Unordered associative containers
219     test_true     <std::unordered_set<int>>();
220     test_true     <std::unordered_map<int, int>>();
221     test_true     <std::unordered_multiset<int>>();
222     test_true     <std::unordered_multimap<int, int>>();
223 
224     // Container adaptors
225     test_true     <std::stack<               int>>();
226     test_true     <std::queue<int>>();
227     test_true     <std::priority_queue<int>>();
228 
229     test_true     <std::span<               int>>();
230     test_true     <std::span<const          int>>();
231     test_true     <std::span<      volatile int>>();
232     test_true     <std::span<const volatile int>>();
233 
234     // Strings
235     test_true     <std::string>();
236 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
237     test_true     <std::wstring>();
238 #endif
239     test_true     <std::u8string>();
240     test_true     <std::u16string>();
241     test_true     <std::u32string>();
242 
243     // String views
244     test_true     <std::string_view>();
245 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
246     test_true     <std::wstring_view>();
247 #endif
248     test_true     <std::u8string_view>();
249     test_true     <std::u16string_view>();
250     test_true     <std::u32string_view>();
251 
252     // Smart pointers
253     test_true     <std::unique_ptr<int>>();
254     test_true     <std::shared_ptr<int>>();
255     test_true     <std::weak_ptr<int>>();
256 
257 }
258