|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | | -// UNSUPPORTED: c++03, c++11, c++14 |
| 9 | +// REQUIRES: std-at-least-c++17 |
10 | 10 | // <optional> |
11 | 11 |
|
12 | 12 | // template <class U, class... Args> |
13 | 13 | // constexpr |
14 | 14 | // explicit optional(in_place_t, initializer_list<U> il, Args&&... args); |
15 | 15 |
|
| 16 | +#include <cassert> |
| 17 | +#include <memory> |
16 | 18 | #include <optional> |
17 | 19 | #include <type_traits> |
18 | | -#include <memory> |
19 | 20 | #include <vector> |
20 | | -#include <cassert> |
21 | 21 |
|
22 | 22 | #include "test_macros.h" |
23 | 23 |
|
24 | | -using std::optional; |
25 | | -using std::in_place_t; |
26 | 24 | using std::in_place; |
| 25 | +using std::in_place_t; |
| 26 | +using std::optional; |
| 27 | + |
| 28 | +class X { |
| 29 | + int i_; |
| 30 | + int j_ = 0; |
27 | 31 |
|
28 | | -class X |
29 | | -{ |
30 | | - int i_; |
31 | | - int j_ = 0; |
32 | 32 | public: |
33 | | - X() : i_(0) {} |
34 | | - X(int i) : i_(i) {} |
35 | | - X(int i, int j) : i_(i), j_(j) {} |
| 33 | + X() : i_(0) {} |
| 34 | + X(int i) : i_(i) {} |
| 35 | + X(int i, int j) : i_(i), j_(j) {} |
36 | 36 |
|
37 | | - ~X() {} |
| 37 | + ~X() {} |
38 | 38 |
|
39 | | - friend bool operator==(const X& x, const X& y) |
40 | | - {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 39 | + friend bool operator==(const X& x, const X& y) { return x.i_ == y.i_ && x.j_ == y.j_; } |
41 | 40 | }; |
42 | 41 |
|
43 | | -class Y |
44 | | -{ |
45 | | - int i_; |
46 | | - int j_ = 0; |
| 42 | +class Y { |
| 43 | + int i_; |
| 44 | + int j_ = 0; |
| 45 | + |
47 | 46 | public: |
48 | | - constexpr Y() : i_(0) {} |
49 | | - constexpr Y(int i) : i_(i) {} |
50 | | - constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} |
| 47 | + constexpr Y() : i_(0) {} |
| 48 | + constexpr Y(int i) : i_(i) {} |
| 49 | + constexpr Y(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) {} |
51 | 50 |
|
52 | | - friend constexpr bool operator==(const Y& x, const Y& y) |
53 | | - {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 51 | + friend constexpr bool operator==(const Y& x, const Y& y) { return x.i_ == y.i_ && x.j_ == y.j_; } |
54 | 52 | }; |
55 | 53 |
|
56 | | -class Z |
57 | | -{ |
58 | | - int i_; |
59 | | - int j_ = 0; |
| 54 | +class Z { |
| 55 | + int i_; |
| 56 | + int j_ = 0; |
| 57 | + |
60 | 58 | public: |
61 | | - Z() : i_(0) {} |
62 | | - Z(int i) : i_(i) {} |
63 | | - Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) |
64 | | - {TEST_THROW(6);} |
| 59 | + Z() : i_(0) {} |
| 60 | + Z(int i) : i_(i) {} |
| 61 | + Z(std::initializer_list<int> il) : i_(il.begin()[0]), j_(il.begin()[1]) { TEST_THROW(6); } |
65 | 62 |
|
66 | | - friend bool operator==(const Z& x, const Z& y) |
67 | | - {return x.i_ == y.i_ && x.j_ == y.j_;} |
| 63 | + friend bool operator==(const Z& x, const Z& y) { return x.i_ == y.i_ && x.j_ == y.j_; } |
68 | 64 | }; |
69 | 65 |
|
70 | | -int main(int, char**) |
71 | | -{ |
72 | | - { |
73 | | - static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, ""); |
74 | | - static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, ""); |
75 | | - } |
76 | | - { |
77 | | - optional<std::vector<int>> opt(in_place, {3, 1}); |
78 | | - assert(static_cast<bool>(opt) == true); |
79 | | - assert((*opt == std::vector<int>{3, 1})); |
80 | | - assert(opt->size() == 2); |
81 | | - } |
82 | | - { |
83 | | - optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>()); |
84 | | - assert(static_cast<bool>(opt) == true); |
85 | | - assert((*opt == std::vector<int>{3, 1})); |
86 | | - assert(opt->size() == 2); |
87 | | - } |
88 | | - { |
89 | | - static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, ""); |
90 | | - constexpr optional<Y> opt(in_place, {3, 1}); |
91 | | - static_assert(static_cast<bool>(opt) == true, ""); |
92 | | - static_assert(*opt == Y{3, 1}, ""); |
93 | | - |
94 | | - struct test_constexpr_ctor |
95 | | - : public optional<Y> |
96 | | - { |
97 | | - constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i) |
98 | | - : optional<Y>(in_place, i) {} |
99 | | - }; |
100 | | - |
101 | | - } |
| 66 | +int main(int, char**) { |
| 67 | + { |
| 68 | + static_assert(!std::is_constructible<X, std::initializer_list<int>&>::value, ""); |
| 69 | + static_assert(!std::is_constructible<optional<X>, std::initializer_list<int>&>::value, ""); |
| 70 | + } |
| 71 | + { |
| 72 | + optional<std::vector<int>> opt(in_place, {3, 1}); |
| 73 | + assert(static_cast<bool>(opt) == true); |
| 74 | + assert((*opt == std::vector<int>{3, 1})); |
| 75 | + assert(opt->size() == 2); |
| 76 | + } |
| 77 | + { |
| 78 | + optional<std::vector<int>> opt(in_place, {3, 1}, std::allocator<int>()); |
| 79 | + assert(static_cast<bool>(opt) == true); |
| 80 | + assert((*opt == std::vector<int>{3, 1})); |
| 81 | + assert(opt->size() == 2); |
| 82 | + } |
| 83 | + { |
| 84 | + static_assert(std::is_constructible<optional<Y>, std::initializer_list<int>&>::value, ""); |
| 85 | + constexpr optional<Y> opt(in_place, {3, 1}); |
| 86 | + static_assert(static_cast<bool>(opt) == true, ""); |
| 87 | + static_assert(*opt == Y{3, 1}, ""); |
| 88 | + |
| 89 | + struct test_constexpr_ctor : public optional<Y> { |
| 90 | + constexpr test_constexpr_ctor(in_place_t, std::initializer_list<int> i) : optional<Y>(in_place, i) {} |
| 91 | + }; |
| 92 | + } |
102 | 93 | #ifndef TEST_HAS_NO_EXCEPTIONS |
103 | | - { |
104 | | - static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, ""); |
105 | | - try |
106 | | - { |
107 | | - optional<Z> opt(in_place, {3, 1}); |
108 | | - assert(false); |
109 | | - } |
110 | | - catch (int i) |
111 | | - { |
112 | | - assert(i == 6); |
113 | | - } |
| 94 | + { |
| 95 | + static_assert(std::is_constructible<optional<Z>, std::initializer_list<int>&>::value, ""); |
| 96 | + try { |
| 97 | + optional<Z> opt(in_place, {3, 1}); |
| 98 | + assert(false); |
| 99 | + } catch (int i) { |
| 100 | + assert(i == 6); |
114 | 101 | } |
| 102 | + } |
115 | 103 | #endif |
116 | 104 |
|
117 | 105 | return 0; |
|
0 commit comments