|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 | // |
9 | | -// UNSUPPORTED: c++03, c++11, c++14 |
| 9 | +// REQUIRED: std-at-least-c++17 |
10 | 10 |
|
11 | 11 | // <optional> |
12 | 12 |
|
13 | 13 | // template <class... Args> |
14 | 14 | // constexpr explicit optional(in_place_t, Args&&... args); |
15 | 15 |
|
| 16 | +#include <cassert> |
16 | 17 | #include <optional> |
17 | 18 | #include <type_traits> |
18 | | -#include <cassert> |
| 19 | +#include <utility> |
19 | 20 |
|
20 | 21 | #include "test_macros.h" |
21 | 22 |
|
22 | | -using std::optional; |
23 | | -using std::in_place_t; |
24 | 23 | using std::in_place; |
| 24 | +using std::in_place_t; |
| 25 | +using std::optional; |
25 | 26 |
|
26 | | -class X |
27 | | -{ |
28 | | - int i_; |
29 | | - int j_ = 0; |
30 | | -public: |
31 | | - X() : i_(0) {} |
32 | | - X(int i) : i_(i) {} |
33 | | - X(int i, int j) : i_(i), j_(j) {} |
34 | | - |
35 | | - ~X() {} |
36 | | - |
37 | | - friend bool operator==(const X& x, const X& y) |
38 | | - {return x.i_ == y.i_ && x.j_ == y.j_;} |
39 | | -}; |
| 27 | +class X { |
| 28 | + int i_; |
| 29 | + int j_ = 0; |
40 | 30 |
|
41 | | -class Y |
42 | | -{ |
43 | | - int i_; |
44 | | - int j_ = 0; |
45 | 31 | public: |
46 | | - constexpr Y() : i_(0) {} |
47 | | - constexpr Y(int i) : i_(i) {} |
48 | | - constexpr Y(int i, int j) : i_(i), j_(j) {} |
| 32 | + constexpr X() : i_(0) {} |
| 33 | + constexpr X(int i) : i_(i) {} |
| 34 | + constexpr X(int i, int j) : i_(i), j_(j) {} |
49 | 35 |
|
50 | | - friend constexpr bool operator==(const Y& x, const Y& y) |
51 | | - {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 36 | + ~X() = default; |
| 37 | + |
| 38 | + friend constexpr bool operator==(const X& x, const X& y) { return x.i_ == y.i_ && x.j_ == y.j_; } |
52 | 39 | }; |
53 | 40 |
|
54 | | -class Z |
55 | | -{ |
| 41 | +class Z { |
56 | 42 | public: |
57 | | - Z(int) {TEST_THROW(6);} |
| 43 | + Z(int) { TEST_THROW(6); } |
58 | 44 | }; |
59 | 45 |
|
| 46 | +template <typename T, typename... Args> |
| 47 | +constexpr void test_inplace(Args... args) { |
| 48 | + optional<T> opt(in_place, args...); |
| 49 | + assert(bool(opt)); |
| 50 | + assert(*opt == T(args...)); |
60 | 51 |
|
61 | | -int main(int, char**) |
62 | | -{ |
63 | | - { |
64 | | - constexpr optional<int> opt(in_place, 5); |
65 | | - static_assert(static_cast<bool>(opt) == true, ""); |
66 | | - static_assert(*opt == 5, ""); |
67 | | - |
68 | | - struct test_constexpr_ctor |
69 | | - : public optional<int> |
70 | | - { |
71 | | - constexpr test_constexpr_ctor(in_place_t, int i) |
72 | | - : optional<int>(in_place, i) {} |
73 | | - }; |
74 | | - |
75 | | - } |
76 | | - { |
77 | | - optional<const int> opt(in_place, 5); |
78 | | - assert(*opt == 5); |
79 | | - } |
80 | | - { |
81 | | - const optional<X> opt(in_place); |
82 | | - assert(static_cast<bool>(opt) == true); |
83 | | - assert(*opt == X()); |
84 | | - } |
85 | | - { |
86 | | - const optional<X> opt(in_place, 5); |
87 | | - assert(static_cast<bool>(opt) == true); |
88 | | - assert(*opt == X(5)); |
89 | | - } |
90 | | - { |
91 | | - const optional<X> opt(in_place, 5, 4); |
92 | | - assert(static_cast<bool>(opt) == true); |
93 | | - assert(*opt == X(5, 4)); |
94 | | - } |
95 | | - { |
96 | | - constexpr optional<Y> opt(in_place); |
97 | | - static_assert(static_cast<bool>(opt) == true, ""); |
98 | | - static_assert(*opt == Y(), ""); |
99 | | - |
100 | | - struct test_constexpr_ctor |
101 | | - : public optional<Y> |
102 | | - { |
103 | | - constexpr test_constexpr_ctor(in_place_t) |
104 | | - : optional<Y>(in_place) {} |
105 | | - }; |
106 | | - |
107 | | - } |
108 | | - { |
109 | | - constexpr optional<Y> opt(in_place, 5); |
110 | | - static_assert(static_cast<bool>(opt) == true, ""); |
111 | | - static_assert(*opt == Y(5), ""); |
112 | | - |
113 | | - struct test_constexpr_ctor |
114 | | - : public optional<Y> |
115 | | - { |
116 | | - constexpr test_constexpr_ctor(in_place_t, int i) |
117 | | - : optional<Y>(in_place, i) {} |
118 | | - }; |
119 | | - |
120 | | - } |
121 | | - { |
122 | | - constexpr optional<Y> opt(in_place, 5, 4); |
123 | | - static_assert(static_cast<bool>(opt) == true, ""); |
124 | | - static_assert(*opt == Y(5, 4), ""); |
125 | | - |
126 | | - struct test_constexpr_ctor |
127 | | - : public optional<Y> |
128 | | - { |
129 | | - constexpr test_constexpr_ctor(in_place_t, int i, int j) |
130 | | - : optional<Y>(in_place, i, j) {} |
131 | | - }; |
| 52 | + struct test_constexpr_ctor : public optional<int> { |
| 53 | + constexpr test_constexpr_ctor(in_place_t, int i) : optional<int>(in_place, i) {} |
| 54 | + }; |
| 55 | +} |
132 | 56 |
|
133 | | - } |
| 57 | +TEST_CONSTEXPR_CXX26 void test_throwing() { |
134 | 58 | #ifndef TEST_HAS_NO_EXCEPTIONS |
135 | | - { |
136 | | - try |
137 | | - { |
138 | | - const optional<Z> opt(in_place, 1); |
139 | | - assert(false); |
140 | | - } |
141 | | - catch (int i) |
142 | | - { |
143 | | - assert(i == 6); |
144 | | - } |
| 59 | + { |
| 60 | + try { |
| 61 | + const optional<Z> opt(in_place, 1); |
| 62 | + assert(false); |
| 63 | + } catch (int i) { |
| 64 | + assert(i == 6); |
145 | 65 | } |
| 66 | + } |
146 | 67 | #endif |
| 68 | +} |
| 69 | + |
| 70 | +constexpr bool test() { |
| 71 | + test_inplace<int>(5); |
| 72 | + test_inplace<const int>(5); |
| 73 | + test_inplace<X>(); |
| 74 | + test_inplace<X>(5); |
| 75 | + test_inplace<X>(5, 4); |
| 76 | +#if TEST_STD_VER >= 26 && 0 |
| 77 | + test_throwing(); |
| 78 | +#endif |
| 79 | + return true; |
| 80 | +} |
| 81 | + |
| 82 | +int main(int, char**) { |
| 83 | + test(); |
| 84 | + static_assert(test()); |
147 | 85 |
|
| 86 | + { |
| 87 | + test_throwing(); |
| 88 | + } |
148 | 89 | return 0; |
149 | 90 | } |
0 commit comments