|
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 | // constexpr optional(T&& v); |
14 | 14 |
|
| 15 | +#include <cassert> |
15 | 16 | #include <optional> |
16 | 17 | #include <type_traits> |
17 | | -#include <cassert> |
18 | 18 |
|
19 | 19 | #include "test_macros.h" |
20 | 20 | #include "archetypes.h" |
21 | 21 |
|
22 | | - |
23 | 22 | using std::optional; |
24 | 23 |
|
25 | | - |
26 | | -class Z |
27 | | -{ |
| 24 | +class Z { |
28 | 25 | public: |
29 | | - Z(int) {} |
30 | | - Z(Z&&) {TEST_THROW(6);} |
| 26 | + Z(int) {} |
| 27 | + Z(Z&&) { TEST_THROW(6); } |
31 | 28 | }; |
32 | 29 |
|
33 | | - |
34 | | -int main(int, char**) |
35 | | -{ |
36 | | - { |
37 | | - typedef int T; |
38 | | - constexpr optional<T> opt(T(5)); |
39 | | - static_assert(static_cast<bool>(opt) == true, ""); |
40 | | - static_assert(*opt == 5, ""); |
41 | | - |
42 | | - struct test_constexpr_ctor |
43 | | - : public optional<T> |
44 | | - { |
45 | | - constexpr test_constexpr_ctor(T&&) {} |
46 | | - }; |
47 | | - } |
48 | | - { |
49 | | - typedef double T; |
50 | | - constexpr optional<T> opt(T(3)); |
51 | | - static_assert(static_cast<bool>(opt) == true, ""); |
52 | | - static_assert(*opt == 3, ""); |
53 | | - |
54 | | - struct test_constexpr_ctor |
55 | | - : public optional<T> |
56 | | - { |
57 | | - constexpr test_constexpr_ctor(T&&) {} |
58 | | - }; |
59 | | - } |
60 | | - { |
61 | | - const int x = 42; |
62 | | - optional<const int> o(std::move(x)); |
63 | | - assert(*o == 42); |
64 | | - } |
65 | | - { |
66 | | - typedef TestTypes::TestType T; |
67 | | - T::reset(); |
68 | | - optional<T> opt = T{3}; |
69 | | - assert(T::alive == 1); |
70 | | - assert(T::move_constructed == 1); |
71 | | - assert(static_cast<bool>(opt) == true); |
72 | | - assert(opt.value().value == 3); |
73 | | - } |
74 | | - { |
75 | | - typedef ExplicitTestTypes::TestType T; |
76 | | - static_assert(!std::is_convertible<T&&, optional<T>>::value, ""); |
77 | | - T::reset(); |
78 | | - optional<T> opt(T{3}); |
79 | | - assert(T::alive == 1); |
80 | | - assert(T::move_constructed == 1); |
81 | | - assert(static_cast<bool>(opt) == true); |
82 | | - assert(opt.value().value == 3); |
83 | | - } |
84 | | - { |
85 | | - typedef TestTypes::TestType T; |
86 | | - T::reset(); |
87 | | - optional<T> opt = {3}; |
88 | | - assert(T::alive == 1); |
89 | | - assert(T::value_constructed == 1); |
90 | | - assert(T::copy_constructed == 0); |
91 | | - assert(T::move_constructed == 0); |
92 | | - assert(static_cast<bool>(opt) == true); |
93 | | - assert(opt.value().value == 3); |
94 | | - } |
95 | | - { |
96 | | - typedef ConstexprTestTypes::TestType T; |
97 | | - constexpr optional<T> opt = {T(3)}; |
98 | | - static_assert(static_cast<bool>(opt) == true, ""); |
99 | | - static_assert(opt.value().value == 3, ""); |
100 | | - |
101 | | - struct test_constexpr_ctor |
102 | | - : public optional<T> |
103 | | - { |
104 | | - constexpr test_constexpr_ctor(const T&) {} |
105 | | - }; |
106 | | - } |
107 | | - { |
108 | | - typedef ConstexprTestTypes::TestType T; |
109 | | - constexpr optional<T> opt = {3}; |
110 | | - static_assert(static_cast<bool>(opt) == true, ""); |
111 | | - static_assert(opt.value().value == 3, ""); |
112 | | - |
113 | | - struct test_constexpr_ctor |
114 | | - : public optional<T> |
115 | | - { |
116 | | - constexpr test_constexpr_ctor(const T&) {} |
117 | | - }; |
118 | | - } |
119 | | - { |
120 | | - typedef ExplicitConstexprTestTypes::TestType T; |
121 | | - static_assert(!std::is_convertible<T&&, optional<T>>::value, ""); |
122 | | - constexpr optional<T> opt(T{3}); |
123 | | - static_assert(static_cast<bool>(opt) == true, ""); |
124 | | - static_assert(opt.value().value == 3, ""); |
125 | | - |
126 | | - struct test_constexpr_ctor |
127 | | - : public optional<T> |
128 | | - { |
129 | | - constexpr test_constexpr_ctor(T&&) {} |
130 | | - }; |
131 | | - |
132 | | - } |
| 30 | +int main(int, char**) { |
| 31 | + { |
| 32 | + typedef int T; |
| 33 | + constexpr optional<T> opt(T(5)); |
| 34 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 35 | + static_assert(*opt == 5, ""); |
| 36 | + |
| 37 | + struct test_constexpr_ctor : public optional<T> { |
| 38 | + constexpr test_constexpr_ctor(T&&) {} |
| 39 | + }; |
| 40 | + } |
| 41 | + { |
| 42 | + typedef double T; |
| 43 | + constexpr optional<T> opt(T(3)); |
| 44 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 45 | + static_assert(*opt == 3, ""); |
| 46 | + |
| 47 | + struct test_constexpr_ctor : public optional<T> { |
| 48 | + constexpr test_constexpr_ctor(T&&) {} |
| 49 | + }; |
| 50 | + } |
| 51 | + { |
| 52 | + const int x = 42; |
| 53 | + optional<const int> o(std::move(x)); |
| 54 | + assert(*o == 42); |
| 55 | + } |
| 56 | + { |
| 57 | + typedef TestTypes::TestType T; |
| 58 | + T::reset(); |
| 59 | + optional<T> opt = T{3}; |
| 60 | + assert(T::alive == 1); |
| 61 | + assert(T::move_constructed == 1); |
| 62 | + assert(static_cast<bool>(opt) == true); |
| 63 | + assert(opt.value().value == 3); |
| 64 | + } |
| 65 | + { |
| 66 | + typedef ExplicitTestTypes::TestType T; |
| 67 | + static_assert(!std::is_convertible<T&&, optional<T>>::value, ""); |
| 68 | + T::reset(); |
| 69 | + optional<T> opt(T{3}); |
| 70 | + assert(T::alive == 1); |
| 71 | + assert(T::move_constructed == 1); |
| 72 | + assert(static_cast<bool>(opt) == true); |
| 73 | + assert(opt.value().value == 3); |
| 74 | + } |
| 75 | + { |
| 76 | + typedef TestTypes::TestType T; |
| 77 | + T::reset(); |
| 78 | + optional<T> opt = {3}; |
| 79 | + assert(T::alive == 1); |
| 80 | + assert(T::value_constructed == 1); |
| 81 | + assert(T::copy_constructed == 0); |
| 82 | + assert(T::move_constructed == 0); |
| 83 | + assert(static_cast<bool>(opt) == true); |
| 84 | + assert(opt.value().value == 3); |
| 85 | + } |
| 86 | + { |
| 87 | + typedef ConstexprTestTypes::TestType T; |
| 88 | + constexpr optional<T> opt = {T(3)}; |
| 89 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 90 | + static_assert(opt.value().value == 3, ""); |
| 91 | + |
| 92 | + struct test_constexpr_ctor : public optional<T> { |
| 93 | + constexpr test_constexpr_ctor(const T&) {} |
| 94 | + }; |
| 95 | + } |
| 96 | + { |
| 97 | + typedef ConstexprTestTypes::TestType T; |
| 98 | + constexpr optional<T> opt = {3}; |
| 99 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 100 | + static_assert(opt.value().value == 3, ""); |
| 101 | + |
| 102 | + struct test_constexpr_ctor : public optional<T> { |
| 103 | + constexpr test_constexpr_ctor(const T&) {} |
| 104 | + }; |
| 105 | + } |
| 106 | + { |
| 107 | + typedef ExplicitConstexprTestTypes::TestType T; |
| 108 | + static_assert(!std::is_convertible<T&&, optional<T>>::value, ""); |
| 109 | + constexpr optional<T> opt(T{3}); |
| 110 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 111 | + static_assert(opt.value().value == 3, ""); |
| 112 | + |
| 113 | + struct test_constexpr_ctor : public optional<T> { |
| 114 | + constexpr test_constexpr_ctor(T&&) {} |
| 115 | + }; |
| 116 | + } |
133 | 117 | #ifndef TEST_HAS_NO_EXCEPTIONS |
134 | | - { |
135 | | - try |
136 | | - { |
137 | | - Z z(3); |
138 | | - optional<Z> opt(std::move(z)); |
139 | | - assert(false); |
140 | | - } |
141 | | - catch (int i) |
142 | | - { |
143 | | - assert(i == 6); |
144 | | - } |
| 118 | + { |
| 119 | + try { |
| 120 | + Z z(3); |
| 121 | + optional<Z> opt(std::move(z)); |
| 122 | + assert(false); |
| 123 | + } catch (int i) { |
| 124 | + assert(i == 6); |
145 | 125 | } |
| 126 | + } |
146 | 127 | #endif |
147 | 128 |
|
148 | 129 | return 0; |
|
0 commit comments