summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/optional/requirements.cc
blob: d6a81529c748ba1b4fff28d8da4e9c73b5d9b6c2 (plain)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// { dg-do run { target c++17 }  }

// Copyright (C) 2013-2022 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include <optional>

#ifndef __cpp_lib_optional
# error "Feature test macro for optional is missing in <optional>"
#elif __cpp_lib_optional < 201606L
# error "Feature test macro for optional has wrong value in <optional>"
#elif __cplusplus >= 202002L && __cpp_lib_optional < 202106L
# error "Feature test macro for optional has wrong value for C++20 in <optional>"
#endif

#include <testsuite_hooks.h>

#include <tuple>

using std::bad_optional_access;
static_assert( std::is_default_constructible<bad_optional_access>::value, "" );

struct trivially_destructible
{
  trivially_destructible() = delete;
  trivially_destructible(trivially_destructible const&) = delete;
  trivially_destructible& operator=(trivially_destructible const&) = delete;
  trivially_destructible(trivially_destructible&&) = delete;
  trivially_destructible& operator=(trivially_destructible&&) = delete;
  ~trivially_destructible() noexcept = default;
};

static_assert( std::is_trivially_destructible<trivially_destructible>(), "" );

struct no_default_constructor
{
  no_default_constructor() = delete;
};

struct no_copy_constructor
{
  no_copy_constructor() = default;
  no_copy_constructor(no_copy_constructor const&) = delete;
  no_copy_constructor& operator=(no_copy_constructor const&) = default;
  no_copy_constructor(no_copy_constructor&&) = default;
  no_copy_constructor& operator=(no_copy_constructor&&) = default;
};

struct no_copy_assignment
{
  no_copy_assignment() = default;
  no_copy_assignment(no_copy_assignment const&) = default;
  no_copy_assignment(no_copy_assignment&&) = default;
  no_copy_assignment& operator=(no_copy_assignment&&) = default;
};

struct no_move_constructor
{
  no_move_constructor() = default;
  no_move_constructor(no_move_constructor const&) = default;
  no_move_constructor& operator=(no_move_constructor const&) = default;
  no_move_constructor(no_move_constructor&&) = delete;
  no_move_constructor& operator=(no_move_constructor&&) = default;
};

struct no_move_assignment
{
  no_move_assignment() = default;
  no_move_assignment(no_move_assignment const&) = default;
  no_move_assignment& operator=(no_move_assignment const&) = default;
  no_move_assignment(no_move_assignment&&) = default;
  no_move_assignment& operator=(no_move_assignment&&) = delete;
};

struct no_copy : no_copy_constructor, no_copy_assignment { };
struct no_move : no_move_constructor, no_move_assignment { };

// Laxest possible model of a value type for optional
struct only_destructible
{
  only_destructible(only_destructible&&) = delete;
};

int main()
{
  {
    static_assert( std::is_trivially_destructible<std::optional<trivially_destructible>>(), "" );
  }

  {
    using T = no_default_constructor;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( std::is_copy_constructible<O>(), "" );
    { O o; auto copy = o; }
    static_assert( std::is_copy_assignable<O>(), "" );
    { O o, p; p = o; }
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_copy_constructor;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( !std::is_copy_constructible<O>(), "" );
    static_assert( !std::is_copy_assignable<O>(), "" );
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_copy_assignment;
    using O = std::optional<T>;
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( std::is_copy_constructible<O>(), "" );
    { O o; auto copy = o; }
    static_assert( !std::is_copy_assignable<O>(), "" );
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_copy;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( !std::is_copy_constructible<O>(), "" );
    static_assert( !std::is_copy_assignable<O>(), "" );
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_move_constructor;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( std::is_copy_constructible<O>(), "" );
    { O o; auto copy = o; }
    static_assert( std::is_copy_assignable<O>(), "" );
    /*
     * T should be move constructible due to [12.8/11], which is a new rule in C++1y
     * not yet implemented by GCC. Because there is already a special exception in C++11
     * for the generation of the special members that GCC implements (at least some of the
     * time), this does not affect the std::optional implementation however. So the assertion
     * for T should be changed (or removed altogether) when the time comes, but the rest
     * should however remain correct and unchanged.
     */
    static_assert( !std::is_move_constructible<T>(), "" );
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_move_assignment;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( std::is_copy_constructible<O>(), "" );
    { O o; auto copy = o; }
    static_assert( std::is_copy_assignable<O>(), "" );
    { O o, p; p = o; }
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    /*
     * Paragraph 23 of same leads to a similar situation but with respect to move
     * assignment.
     */
    static_assert( !std::is_move_assignable<T>(), "" );
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = no_move;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( std::is_copy_constructible<O>(), "" );
    { O o; auto copy = o; }
    static_assert( std::is_copy_assignable<O>(), "" );
    { O o, p; p = o; }
    static_assert( std::is_move_constructible<O>(), "" );
    { O o; auto moved_to = std::move(o); }
    static_assert( std::is_move_assignable<O>(), "" );
    { O o, p; p = std::move(o); }
  }

  {
    using T = only_destructible;
    using O = std::optional<T>;
    static_assert( std::is_same<O::value_type, T>(), "" );
    static_assert( std::is_default_constructible<O>(), "" );
    { O o; }
    static_assert( !std::is_copy_constructible<O>(), "" );
    static_assert( !std::is_copy_assignable<O>(), "" );
    static_assert( !std::is_move_constructible<O>(), "" );
    static_assert( !std::is_move_assignable<O>(), "" );
  }

  {
    /*
     * Should not complain about 'invalid' specializations as long as
     * they're not instantiated.
     */
    using A = std::optional<int&>;
    using B = std::optional<int&&>;
    using C1 = std::optional<std::in_place_t>;
    using C2 = std::optional<std::in_place_t const>;
    using C3 = std::optional<std::in_place_t volatile>;
    using C4 = std::optional<std::in_place_t const volatile>;
    using D1 = std::optional<std::nullopt_t>;
    using D2 = std::optional<std::nullopt_t const>;
    using D3 = std::optional<std::nullopt_t volatile>;
    using D4 = std::optional<std::nullopt_t const volatile>;

    using X = std::tuple<A, B, C1, C2, C3, C4, D1, D2, D3, D4>;
  }

  {
    std::optional<const int> o { 42 };
    static_assert( std::is_same<decltype(o)::value_type, const int>(), "" );
    VERIFY( o );
    VERIFY( *o == 42 );
  }

  {
    constexpr std::optional<const int> o { 33 };
    static_assert( std::is_same<decltype(o)::value_type, const int>(), "" );
    static_assert( o, "" );
    static_assert( *o == 33, "" );
  }
}

using std::void_t;
using std::declval;
using std::true_type;
using std::false_type;

template <class T, class = void>
struct is_eq_comparable : false_type {};
template <class T>
struct is_eq_comparable<T, void_t<decltype(declval<T>() == declval<T>())>>
: true_type {};

template <class T, class = void>
struct is_neq_comparable : false_type {};
template <class T>
struct is_neq_comparable<T, void_t<decltype(declval<T>() != declval<T>())>>
: true_type {};

template <class T, class = void>
struct is_lt_comparable : false_type {};
template <class T>
struct is_lt_comparable<T, void_t<decltype(declval<T>() < declval<T>())>>
: true_type {};

template <class T, class = void>
struct is_gt_comparable : false_type {};
template <class T>
struct is_gt_comparable<T, void_t<decltype(declval<T>() > declval<T>())>>
: true_type {};

template <class T, class = void>
struct is_le_comparable : false_type {};
template <class T>
struct is_le_comparable<T, void_t<decltype(declval<T>() <= declval<T>())>>
: true_type {};

template <class T, class = void>
struct is_ge_comparable : false_type {};
template <class T>
struct is_ge_comparable<T, void_t<decltype(declval<T>() >= declval<T>())>>
: true_type {};

using std::optional;

static_assert(is_eq_comparable<optional<int>>::value, "");
static_assert(is_neq_comparable<optional<int>>::value, "");
static_assert(is_lt_comparable<optional<int>>::value, "");
static_assert(is_gt_comparable<optional<int>>::value, "");
static_assert(is_le_comparable<optional<int>>::value, "");
static_assert(is_ge_comparable<optional<int>>::value, "");

struct JustEq {};
bool operator==(const JustEq&, const JustEq&);

static_assert(is_eq_comparable<optional<JustEq>>::value, "");
#if __cplusplus == 201703L
// In C++20 operator!= can be synthesized from operator==
static_assert(!is_neq_comparable<optional<JustEq>>::value, "");
#endif
static_assert(!is_lt_comparable<optional<JustEq>>::value, "");
static_assert(!is_gt_comparable<optional<JustEq>>::value, "");
static_assert(!is_le_comparable<optional<JustEq>>::value, "");
static_assert(!is_ge_comparable<optional<JustEq>>::value, "");

struct JustLt {};
bool operator<(const JustLt&, const JustLt&);

static_assert(!is_eq_comparable<optional<JustLt>>::value, "");
static_assert(!is_neq_comparable<optional<JustLt>>::value, "");
static_assert(is_lt_comparable<optional<JustLt>>::value, "");
static_assert(!is_gt_comparable<optional<JustLt>>::value, "");
static_assert(!is_le_comparable<optional<JustLt>>::value, "");
static_assert(!is_ge_comparable<optional<JustLt>>::value, "");

static_assert(!std::is_assignable<optional<JustEq>&,
	      optional<JustLt>>::value, "");
static_assert(!std::is_assignable<optional<JustEq>&,
	      JustLt>::value, "");
static_assert(!std::is_assignable<optional<JustEq>&,
	      optional<JustLt>&>::value, "");
static_assert(!std::is_assignable<optional<JustEq>&,
	      JustLt&>::value, "");