|
13 | 13 | // template <class U> |
14 | 14 | // constexpr EXPLICIT optional(U&& u); |
15 | 15 |
|
| 16 | +#include <cassert> |
16 | 17 | #include <optional> |
17 | 18 | #include <type_traits> |
18 | | -#include <cassert> |
19 | 19 |
|
20 | 20 | #include "test_macros.h" |
21 | 21 | #include "archetypes.h" |
22 | 22 | #include "test_convertible.h" |
23 | 23 |
|
24 | | - |
25 | 24 | using std::optional; |
26 | 25 |
|
27 | | -struct ImplicitThrow |
28 | | -{ |
29 | | - constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);} |
| 26 | +struct ImplicitThrow { |
| 27 | + constexpr ImplicitThrow(int x) { |
| 28 | + if (x != -1) |
| 29 | + TEST_THROW(6); |
| 30 | + } |
30 | 31 | }; |
31 | 32 |
|
32 | | -struct ExplicitThrow |
33 | | -{ |
34 | | - constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);} |
| 33 | +struct ExplicitThrow { |
| 34 | + constexpr explicit ExplicitThrow(int x) { |
| 35 | + if (x != -1) |
| 36 | + TEST_THROW(6); |
| 37 | + } |
35 | 38 | }; |
36 | 39 |
|
37 | 40 | struct ImplicitAny { |
38 | 41 | template <class U> |
39 | 42 | constexpr ImplicitAny(U&&) {} |
40 | 43 | }; |
41 | 44 |
|
42 | | - |
43 | 45 | template <class To, class From> |
44 | | -constexpr bool implicit_conversion(optional<To>&& opt, const From& v) |
45 | | -{ |
46 | | - using O = optional<To>; |
47 | | - static_assert(test_convertible<O, From>(), ""); |
48 | | - static_assert(!test_convertible<O, void*>(), ""); |
49 | | - static_assert(!test_convertible<O, From, int>(), ""); |
50 | | - return opt && *opt == static_cast<To>(v); |
| 46 | +constexpr bool implicit_conversion(optional<To>&& opt, const From& v) { |
| 47 | + using O = optional<To>; |
| 48 | + static_assert(test_convertible<O, From>(), ""); |
| 49 | + static_assert(!test_convertible<O, void*>(), ""); |
| 50 | + static_assert(!test_convertible<O, From, int>(), ""); |
| 51 | + return opt && *opt == static_cast<To>(v); |
51 | 52 | } |
52 | 53 |
|
53 | 54 | template <class To, class Input, class Expect> |
54 | | -constexpr bool explicit_conversion(Input&& in, const Expect& v) |
55 | | -{ |
56 | | - using O = optional<To>; |
57 | | - static_assert(std::is_constructible<O, Input>::value, ""); |
58 | | - static_assert(!std::is_convertible<Input, O>::value, ""); |
59 | | - static_assert(!std::is_constructible<O, void*>::value, ""); |
60 | | - static_assert(!std::is_constructible<O, Input, int>::value, ""); |
61 | | - optional<To> opt(std::forward<Input>(in)); |
62 | | - optional<To> opt2{std::forward<Input>(in)}; |
63 | | - return opt && *opt == static_cast<To>(v) && (opt2 && *opt2 == static_cast<To>(v)); |
| 55 | +constexpr bool explicit_conversion(Input&& in, const Expect& v) { |
| 56 | + using O = optional<To>; |
| 57 | + static_assert(std::is_constructible<O, Input>::value, ""); |
| 58 | + static_assert(!std::is_convertible<Input, O>::value, ""); |
| 59 | + static_assert(!std::is_constructible<O, void*>::value, ""); |
| 60 | + static_assert(!std::is_constructible<O, Input, int>::value, ""); |
| 61 | + optional<To> opt(std::forward<Input>(in)); |
| 62 | + optional<To> opt2{std::forward<Input>(in)}; |
| 63 | + return opt && *opt == static_cast<To>(v) && (opt2 && *opt2 == static_cast<To>(v)); |
64 | 64 | } |
65 | 65 |
|
66 | | -void test_implicit() |
67 | | -{ |
68 | | - { |
69 | | - static_assert(implicit_conversion<long long>(42, 42), ""); |
70 | | - } |
71 | | - { |
72 | | - static_assert(implicit_conversion<long double>(3.14, 3.14), ""); |
73 | | - } |
74 | | - { |
75 | | - int x = 42; |
76 | | - optional<void* const> o(&x); |
77 | | - assert(*o == &x); |
78 | | - } |
79 | | - { |
80 | | - using T = TrivialTestTypes::TestType; |
81 | | - static_assert(implicit_conversion<T>(42, 42), ""); |
82 | | - } |
83 | | - { |
84 | | - using T = TestTypes::TestType; |
85 | | - assert(implicit_conversion<T>(3, T(3))); |
86 | | - } |
87 | | - { |
88 | | - using T = TestTypes::TestType; |
89 | | - optional<T> opt({3}); |
90 | | - assert(opt && *opt == static_cast<T>(3)); |
91 | | - } |
| 66 | +void test_implicit() { |
| 67 | + { |
| 68 | + static_assert(implicit_conversion<long long>(42, 42), ""); |
| 69 | + } |
| 70 | + { |
| 71 | + static_assert(implicit_conversion<long double>(3.14, 3.14), ""); |
| 72 | + } |
| 73 | + { |
| 74 | + int x = 42; |
| 75 | + optional<void* const> o(&x); |
| 76 | + assert(*o == &x); |
| 77 | + } |
| 78 | + { |
| 79 | + using T = TrivialTestTypes::TestType; |
| 80 | + static_assert(implicit_conversion<T>(42, 42), ""); |
| 81 | + } |
| 82 | + { |
| 83 | + using T = TestTypes::TestType; |
| 84 | + assert(implicit_conversion<T>(3, T(3))); |
| 85 | + } |
| 86 | + { |
| 87 | + using T = TestTypes::TestType; |
| 88 | + optional<T> opt({3}); |
| 89 | + assert(opt && *opt == static_cast<T>(3)); |
| 90 | + } |
92 | 91 | { |
93 | 92 | using O = optional<ImplicitAny>; |
94 | 93 | static_assert(!test_convertible<O, std::in_place_t>(), ""); |
95 | 94 | static_assert(!test_convertible<O, std::in_place_t&>(), ""); |
96 | 95 | static_assert(!test_convertible<O, const std::in_place_t&>(), ""); |
97 | 96 | static_assert(!test_convertible<O, std::in_place_t&&>(), ""); |
98 | 97 | static_assert(!test_convertible<O, const std::in_place_t&&>(), ""); |
99 | | - |
100 | 98 | } |
101 | 99 | #ifndef TEST_HAS_NO_EXCEPTIONS |
102 | | - { |
103 | | - try { |
104 | | - using T = ImplicitThrow; |
105 | | - optional<T> t = 42; |
106 | | - assert(false); |
107 | | - ((void)t); |
108 | | - } catch (int) { |
109 | | - } |
| 100 | + { |
| 101 | + try { |
| 102 | + using T = ImplicitThrow; |
| 103 | + optional<T> t = 42; |
| 104 | + assert(false); |
| 105 | + ((void)t); |
| 106 | + } catch (int) { |
110 | 107 | } |
| 108 | + } |
111 | 109 | #endif |
112 | 110 | } |
113 | 111 |
|
114 | 112 | void test_explicit() { |
| 113 | + { |
| 114 | + using T = ExplicitTrivialTestTypes::TestType; |
| 115 | + static_assert(explicit_conversion<T>(42, 42), ""); |
| 116 | + } |
| 117 | + { |
| 118 | + using T = ExplicitConstexprTestTypes::TestType; |
| 119 | + static_assert(explicit_conversion<T>(42, 42), ""); |
| 120 | + static_assert(!std::is_convertible<int, T>::value, ""); |
| 121 | + } |
| 122 | + { |
| 123 | + using T = ExplicitTestTypes::TestType; |
| 124 | + T::reset(); |
115 | 125 | { |
116 | | - using T = ExplicitTrivialTestTypes::TestType; |
117 | | - static_assert(explicit_conversion<T>(42, 42), ""); |
118 | | - } |
119 | | - { |
120 | | - using T = ExplicitConstexprTestTypes::TestType; |
121 | | - static_assert(explicit_conversion<T>(42, 42), ""); |
122 | | - static_assert(!std::is_convertible<int, T>::value, ""); |
| 126 | + assert(explicit_conversion<T>(42, 42)); |
| 127 | + assert(T::alive == 0); |
123 | 128 | } |
| 129 | + T::reset(); |
124 | 130 | { |
125 | | - using T = ExplicitTestTypes::TestType; |
126 | | - T::reset(); |
127 | | - { |
128 | | - assert(explicit_conversion<T>(42, 42)); |
129 | | - assert(T::alive == 0); |
130 | | - } |
131 | | - T::reset(); |
132 | | - { |
133 | | - optional<T> t(42); |
134 | | - assert(T::alive == 1); |
135 | | - assert(T::value_constructed == 1); |
136 | | - assert(T::move_constructed == 0); |
137 | | - assert(T::copy_constructed == 0); |
138 | | - assert(t.value().value == 42); |
139 | | - } |
140 | | - assert(T::alive == 0); |
| 131 | + optional<T> t(42); |
| 132 | + assert(T::alive == 1); |
| 133 | + assert(T::value_constructed == 1); |
| 134 | + assert(T::move_constructed == 0); |
| 135 | + assert(T::copy_constructed == 0); |
| 136 | + assert(t.value().value == 42); |
141 | 137 | } |
| 138 | + assert(T::alive == 0); |
| 139 | + } |
142 | 140 | #ifndef TEST_HAS_NO_EXCEPTIONS |
143 | | - { |
144 | | - try { |
145 | | - using T = ExplicitThrow; |
146 | | - optional<T> t(42); |
147 | | - assert(false); |
148 | | - } catch (int) { |
149 | | - } |
| 141 | + { |
| 142 | + try { |
| 143 | + using T = ExplicitThrow; |
| 144 | + optional<T> t(42); |
| 145 | + assert(false); |
| 146 | + } catch (int) { |
150 | 147 | } |
| 148 | + } |
151 | 149 | #endif |
152 | 150 | } |
153 | 151 |
|
154 | 152 | int main(int, char**) { |
155 | | - test_implicit(); |
156 | | - test_explicit(); |
| 153 | + test_implicit(); |
| 154 | + test_explicit(); |
157 | 155 |
|
158 | 156 | return 0; |
159 | 157 | } |
0 commit comments