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