| 4 | #include <stack> |
| 5 | |
| 6 | int32_t main() |
| 7 | { |
| 8 | std::stack<int32_t> stack; |
| 9 | |
| 10 | stack.push(1); |
| 11 | stack.push(2); |
| 12 | stack.push(3); |
| 13 | |
| 14 | auto const safePop = [](std::stack<int32_t> &s) -> std::optional<int32_t> |
| 15 | { |
| 16 | auto const result = std::optional<int32_t>{}; |
| 17 | try |
| 18 | { |
| 19 | if (s.empty()) |
| 20 | { |
| 21 | throw "empty"; |
| 22 | } |
| 23 | auto top = s.top(); |
| 24 | s.pop(); |
| 25 | return top; |
| 26 | } |
| 27 | catch (...) |
| 28 | { |
| 29 | return result; |
| 30 | } |
| 31 | }; |
| 32 | |
| 33 | using namespace matchit; |
| 34 | Id<int32_t> top; |
| 35 | while (match(safePop(stack))( |
| 36 | pattern | some(top) = |
| 37 | [&] |
| 38 | { |
| 39 | std::cout << *top << std::endl; |
| 40 | return true; |
| 41 | }, |
| 42 | pattern | _ = expr(false))) |
| 43 | { |
| 44 | }; |
| 45 | } |