145
|
1 // Testcase from P0848R0
|
|
2 // { dg-do compile { target concepts } }
|
|
3
|
|
4 #include <type_traits>
|
|
5
|
|
6 template <typename T>
|
|
7 class optional
|
|
8 {
|
|
9 struct empty {};
|
|
10 union {
|
|
11 empty _ = { };
|
|
12 T value;
|
|
13 };
|
|
14 bool engaged = false;
|
|
15
|
|
16 public:
|
|
17 constexpr optional() = default;
|
|
18
|
|
19 constexpr optional(optional const&)
|
|
20 requires std::is_trivially_copy_constructible_v<T>
|
|
21 = default;
|
|
22 constexpr optional(optional const& o)
|
|
23 : engaged (o.engaged)
|
|
24 {
|
|
25 if (engaged)
|
|
26 new (&value) T (o.value);
|
|
27 }
|
|
28
|
|
29 ~optional()
|
|
30 requires std::is_trivially_destructible_v<T>
|
|
31 = default;
|
|
32 ~optional()
|
|
33 {
|
|
34 if (engaged)
|
|
35 value.~T();
|
|
36 }
|
|
37
|
|
38 // ...
|
|
39 };
|
|
40
|
|
41 struct A { A(); A(const A&); ~A(); };
|
|
42
|
|
43 static_assert(std::is_trivially_copy_constructible_v<optional<int>>);
|
|
44 static_assert(!std::is_trivially_copy_constructible_v<optional<A>>);
|
|
45 static_assert(std::is_trivially_destructible_v<optional<int>>);
|
|
46 static_assert(!std::is_trivially_destructible_v<optional<A>>);
|