File: default_initializable.compile.pass.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (265 lines) | stat: -rw-r--r-- 7,440 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts

// template<class T>
//     concept default_initializable = constructible_from<T> &&
//     requires { T{}; } &&
//     is-default-initializable<T>;

#include <array>
#include <concepts>
#include <deque>
#include <forward_list>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <span>
#include <stack>
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "test_macros.h"

struct Empty {};

struct CtorDefaulted {
  CtorDefaulted() = default;
};
struct CtorDeleted {
  CtorDeleted() = delete;
};
struct DtorDefaulted {
  ~DtorDefaulted() = default;
};
struct DtorDeleted {
  ~DtorDeleted() = delete;
};

struct Noexcept {
  ~Noexcept() noexcept;
};
struct NoexceptTrue {
  ~NoexceptTrue() noexcept(true);
};
struct NoexceptFalse {
  ~NoexceptFalse() noexcept(false);
};

struct CtorProtected {
protected:
  CtorProtected() = default;
};
struct CtorPrivate {
private:
  CtorPrivate() = default;
};
struct DtorProtected {
protected:
  ~DtorProtected() = default;
};
struct DtorPrivate {
private:
  ~DtorPrivate() = default;
};

template <class T>
struct NoexceptDependant {
  ~NoexceptDependant() noexcept(std::is_same_v<T, int>);
};

struct CtorExplicit {
  explicit CtorExplicit() = default;
};
struct CtorArgument {
  CtorArgument(int) {}
};
struct CtorDefaultArgument {
  CtorDefaultArgument(int = 0) {}
};
struct CtorExplicitDefaultArgument {
  explicit CtorExplicitDefaultArgument(int = 0) {}
};

struct Derived : public Empty {};

class Abstract {
  virtual void foo() = 0;
};

class AbstractDestructor {
  virtual ~AbstractDestructor() = 0;
};

class OperatorNewDeleted {
  void* operator new(std::size_t) = delete;
  void operator delete(void* ptr) = delete;
};

[[maybe_unused]] auto Lambda = [](const int&, int&&, double){};

template<class T>
void test_not_const()
{
    static_assert( std::default_initializable<               T>);
    static_assert(!std::default_initializable<const          T>);
    static_assert( std::default_initializable<      volatile T>);
    static_assert(!std::default_initializable<const volatile T>);
}

template<class T>
void test_true()
{
    static_assert( std::default_initializable<               T>);
    static_assert( std::default_initializable<const          T>);
    static_assert( std::default_initializable<      volatile T>);
    static_assert( std::default_initializable<const volatile T>);
}

template<class T>
void test_false()
{
    static_assert(!std::default_initializable<               T>);
    static_assert(!std::default_initializable<const          T>);
    static_assert(!std::default_initializable<      volatile T>);
    static_assert(!std::default_initializable<const volatile T>);
}

void test()
{
    test_not_const<bool>();
    test_not_const<char>();
    test_not_const<int>();
    test_not_const<double>();

    test_false    <void>();
    test_not_const<void*>();

    test_not_const<int*>();
    test_false    <int[]>();
    test_not_const<int[1]>();
    test_false    <int&>();
    test_false    <int&&>();

    test_true     <Empty>();

    test_true     <CtorDefaulted>();
    test_false    <CtorDeleted>();
    test_true     <DtorDefaulted>();
    test_false    <DtorDeleted>();

    test_true     <Noexcept>();
    test_true     <NoexceptTrue>();
    test_false    <NoexceptFalse>();

    test_false    <CtorProtected>();
    test_false    <CtorPrivate>();
    test_false    <DtorProtected>();
    test_false    <DtorPrivate>();

    test_true     <NoexceptDependant<int>>();
    test_false    <NoexceptDependant<double>>();

    test_true     <CtorExplicit>();
    test_false    <CtorArgument>();
    test_true     <CtorDefaultArgument>();
    test_true     <CtorExplicitDefaultArgument>();

    test_true     <Derived>();
    test_false    <Abstract>();
    test_false    <AbstractDestructor>();

    test_true     <OperatorNewDeleted>();

    test_true     <decltype(Lambda)>();
    test_not_const<void(*)(const int&)>();
    test_not_const<void(Empty::*)(const int&)               >();
    test_not_const<void(Empty::*)(const int&) const         >();
    test_not_const<void(Empty::*)(const int&)       volatile>();
    test_not_const<void(Empty::*)(const int&) const volatile>();
    test_not_const<void(Empty::*)(const int&) &>();
    test_not_const<void(Empty::*)(const int&) &&>();
    test_not_const<void(Empty::*)(const int&) noexcept>();
    test_not_const<void(Empty::*)(const int&) noexcept(true)>();
    test_not_const<void(Empty::*)(const int&) noexcept(false)>();

    // Sequence containers
    test_not_const<std::array<               int, 0>>();
    test_not_const<std::array<               int, 1>>();
    test_false    <std::array<const          int, 1>>();
    test_not_const<std::array<      volatile int, 1>>();
    test_false    <std::array<const volatile int, 1>>();
    test_true     <std::deque<               int>>();
#ifdef _LIBCPP_VERSION
    test_true     <std::deque<const          int>>();
    test_true     <std::deque<      volatile int>>();
    test_true     <std::deque<const volatile int>>();
#endif // _LIBCPP_VERSION
    test_true     <std::forward_list<int>>();
    test_true     <std::list<int>>();
    test_true     <std::vector<int>>();

    // Associative containers
    test_true     <std::set<int>>();
    test_true     <std::map<int, int>>();
    test_true     <std::multiset<int>>();
    test_true     <std::multimap<int, int>>();

    // Unordered associative containers
    test_true     <std::unordered_set<int>>();
    test_true     <std::unordered_map<int, int>>();
    test_true     <std::unordered_multiset<int>>();
    test_true     <std::unordered_multimap<int, int>>();

    // Container adaptors
    test_true     <std::stack<               int>>();
#ifdef _LIBCPP_VERSION
    test_true     <std::stack<const          int>>();
    test_true     <std::stack<      volatile int>>();
    test_true     <std::stack<const volatile int>>();
#endif // _LIBCPP_VERSION
    test_true     <std::queue<int>>();
    test_true     <std::priority_queue<int>>();

    test_true     <std::span<               int>>();
    test_true     <std::span<const          int>>();
    test_true     <std::span<      volatile int>>();
    test_true     <std::span<const volatile int>>();

    // Strings
    test_true     <std::string>();
    test_true     <std::wstring>();
    test_true     <std::u8string>();
    test_true     <std::u16string>();
    test_true     <std::u32string>();

    // String views
    test_true     <std::string_view>();
    test_true     <std::wstring_view>();
    test_true     <std::u8string_view>();
    test_true     <std::u16string_view>();
    test_true     <std::u32string_view>();

    // Smart pointers
    test_true     <std::unique_ptr<int>>();
    test_true     <std::shared_ptr<int>>();
    test_true     <std::weak_ptr<int>>();

}

// Required for MSVC internal test runner compatibility.
int main(int, char**) {
    return 0;
}