1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
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
16 // concept default_initializable = constructible_from<T> &&
17 // requires { T{}; } &&
18 // is-default-initializable<T>;
23 #include <forward_list>
32 #include <string_view>
33 #include <unordered_map>
34 #include <unordered_set>
37 #include "test_macros.h"
41 struct CtorDefaulted
{
42 CtorDefaulted() = default;
45 CtorDeleted() = delete;
47 struct DtorDefaulted
{
48 ~DtorDefaulted() = default;
51 ~DtorDeleted() = delete;
58 ~NoexceptTrue() noexcept(true);
60 struct NoexceptFalse
{
61 ~NoexceptFalse() noexcept(false);
64 struct CtorProtected
{
66 CtorProtected() = default;
70 CtorPrivate() = default;
72 struct DtorProtected
{
74 ~DtorProtected() = default;
78 ~DtorPrivate() = default;
82 struct NoexceptDependant
{
83 ~NoexceptDependant() noexcept(std::is_same_v
<T
, int>);
87 explicit CtorExplicit() = default;
92 struct CtorDefaultArgument
{
93 CtorDefaultArgument(int = 0) {}
95 struct CtorExplicitDefaultArgument
{
96 explicit CtorExplicitDefaultArgument(int = 0) {}
99 struct Derived
: public Empty
{};
102 virtual void foo() = 0;
105 class AbstractDestructor
{
106 virtual ~AbstractDestructor() = 0;
109 class OperatorNewDeleted
{
110 void* operator new(std::size_t) = delete;
111 void operator delete(void* ptr
) = delete;
114 [[maybe_unused
]] auto Lambda
= [](const int&, int&&, double){};
117 void test_not_const()
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
>);
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
>);
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
>);
145 test_not_const
<bool>();
146 test_not_const
<char>();
147 test_not_const
<int>();
148 test_not_const
<double>();
151 test_not_const
<void*>();
153 test_not_const
<int*>();
154 test_false
<int[]>();
155 test_not_const
<int[1]>();
157 test_false
<int&&>();
161 test_true
<CtorDefaulted
>();
162 test_false
<CtorDeleted
>();
163 test_true
<DtorDefaulted
>();
164 test_false
<DtorDeleted
>();
166 test_true
<Noexcept
>();
167 test_true
<NoexceptTrue
>();
168 test_false
<NoexceptFalse
>();
170 test_false
<CtorProtected
>();
171 test_false
<CtorPrivate
>();
172 test_false
<DtorProtected
>();
173 test_false
<DtorPrivate
>();
175 test_true
<NoexceptDependant
<int>>();
176 test_false
<NoexceptDependant
<double>>();
178 test_true
<CtorExplicit
>();
179 test_false
<CtorArgument
>();
180 test_true
<CtorDefaultArgument
>();
181 test_true
<CtorExplicitDefaultArgument
>();
183 test_true
<Derived
>();
184 test_false
<Abstract
>();
185 test_false
<AbstractDestructor
>();
187 test_true
<OperatorNewDeleted
>();
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)>();
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 #ifdef _LIBCPP_VERSION
209 test_true
<std::deque
<const int>>();
210 #endif // _LIBCPP_VERSION
211 test_true
<std::forward_list
<int>>();
212 test_true
<std::list
<int>>();
213 test_true
<std::vector
<int>>();
215 // Associative containers
216 test_true
<std::set
<int>>();
217 test_true
<std::map
<int, int>>();
218 test_true
<std::multiset
<int>>();
219 test_true
<std::multimap
<int, int>>();
221 // Unordered associative containers
222 test_true
<std::unordered_set
<int>>();
223 test_true
<std::unordered_map
<int, int>>();
224 test_true
<std::unordered_multiset
<int>>();
225 test_true
<std::unordered_multimap
<int, int>>();
227 // Container adaptors
228 test_true
<std::stack
< int>>();
229 #ifdef _LIBCPP_VERSION
230 test_true
<std::stack
<const int>>();
231 #endif // _LIBCPP_VERSION
232 test_true
<std::queue
<int>>();
233 test_true
<std::priority_queue
<int>>();
235 test_true
<std::span
< int>>();
236 test_true
<std::span
<const int>>();
237 test_true
<std::span
< volatile int>>();
238 test_true
<std::span
<const volatile int>>();
241 test_true
<std::string
>();
242 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
243 test_true
<std::wstring
>();
245 test_true
<std::u8string
>();
246 test_true
<std::u16string
>();
247 test_true
<std::u32string
>();
250 test_true
<std::string_view
>();
251 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
252 test_true
<std::wstring_view
>();
254 test_true
<std::u8string_view
>();
255 test_true
<std::u16string_view
>();
256 test_true
<std::u32string_view
>();
259 test_true
<std::unique_ptr
<int>>();
260 test_true
<std::shared_ptr
<int>>();
261 test_true
<std::weak_ptr
<int>>();