|
18 | 18 |
|
19 | 19 | #include "test_macros.h" |
20 | 20 |
|
21 | | -struct LVal { |
22 | | - constexpr std::optional<int> operator()(int&) { return 1; } |
23 | | - std::optional<int> operator()(const int&) = delete; |
24 | | - std::optional<int> operator()(int&&) = delete; |
25 | | - std::optional<int> operator()(const int&&) = delete; |
26 | | -}; |
27 | | - |
28 | | -struct CLVal { |
29 | | - std::optional<int> operator()(int&) = delete; |
30 | | - constexpr std::optional<int> operator()(const int&) { return 1; } |
31 | | - std::optional<int> operator()(int&&) = delete; |
32 | | - std::optional<int> operator()(const int&&) = delete; |
33 | | -}; |
34 | | - |
35 | | -struct RVal { |
36 | | - std::optional<int> operator()(int&) = delete; |
37 | | - std::optional<int> operator()(const int&) = delete; |
38 | | - constexpr std::optional<int> operator()(int&&) { return 1; } |
39 | | - std::optional<int> operator()(const int&&) = delete; |
40 | | -}; |
41 | | - |
42 | | -struct CRVal { |
43 | | - std::optional<int> operator()(int&) = delete; |
44 | | - std::optional<int> operator()(const int&) = delete; |
45 | | - std::optional<int> operator()(int&&) = delete; |
46 | | - constexpr std::optional<int> operator()(const int&&) { return 1; } |
47 | | -}; |
48 | | - |
49 | 21 | void test() { |
| 22 | + // [optional.bad.access] |
| 23 | + |
50 | 24 | std::bad_optional_access ex; |
51 | 25 |
|
52 | 26 | ex.what(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
53 | 27 |
|
| 28 | + // [optional.optional] |
| 29 | + |
54 | 30 | std::optional<int> opt; |
55 | 31 | const std::optional<int> cOpt; |
56 | 32 |
|
@@ -84,29 +60,31 @@ void test() { |
84 | 60 |
|
85 | 61 | #if TEST_STD_VER >= 23 |
86 | 62 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
87 | | - opt.and_then(LVal{}); |
| 63 | + opt.and_then([](int&) { return std::optional<int>{94}; }); |
88 | 64 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
89 | | - cOpt.and_then(CLVal{}); |
| 65 | + cOpt.and_then([](const int&) { return std::optional<int>{94}; }); |
90 | 66 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
91 | | - std::move(opt).and_then(RVal{}); |
| 67 | + std::move(opt).and_then([](int&&) { return std::optional<int>{94}; }); |
92 | 68 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
93 | | - std::move(cOpt).and_then(CRVal{}); |
| 69 | + std::move(cOpt).and_then([](const int&&) { return std::optional<int>{94}; }); |
94 | 70 |
|
95 | 71 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
96 | | - opt.transform(LVal{}); |
| 72 | + opt.transform([](int&) { return std::optional<int>{94}; }); |
97 | 73 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
98 | | - cOpt.transform(CLVal{}); |
| 74 | + cOpt.transform([](const int&) { return std::optional<int>{94}; }); |
99 | 75 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
100 | | - std::move(opt).transform(RVal{}); |
| 76 | + std::move(opt).transform([](int&&) { return std::optional<int>{94}; }); |
101 | 77 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
102 | | - std::move(cOpt).transform(CRVal{}); |
| 78 | + std::move(cOpt).transform([](const int&&) { return std::optional<int>{94}; }); |
103 | 79 |
|
104 | 80 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
105 | 81 | opt.or_else([] { return std::optional<int>{82}; }); |
106 | 82 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
107 | 83 | std::move(opt).or_else([] { return std::optional<int>{82}; }); |
108 | 84 | #endif // TEST_STD_VER >= 23 |
109 | 85 |
|
| 86 | + // [optional.optional.ref] |
| 87 | + |
110 | 88 | #if TEST_STD_VER >= 26 |
111 | 89 | int z = 94; |
112 | 90 | std::optional<int&> optRef{z}; |
@@ -143,13 +121,17 @@ void test() { |
143 | 121 | std::move(optRef).or_else([] { return std::optional<int&>{}; }); |
144 | 122 | #endif // TEST_STD_VER >= 26 |
145 | 123 |
|
| 124 | + // [optional.specalg] |
| 125 | + |
146 | 126 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
147 | 127 | std::make_optional(82); |
148 | 128 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
149 | 129 | std::make_optional<int>('h'); |
150 | 130 | // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} |
151 | 131 | std::make_optional<std::string>({'z', 'm', 't'}); |
152 | 132 |
|
| 133 | + // [optional.hash] |
| 134 | + |
153 | 135 | std::hash<std::optional<int>> hash; |
154 | 136 |
|
155 | 137 | hash(opt); //expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} |
|
0 commit comments