forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlice-pattern.cpp
37 lines (34 loc) · 869 Bytes
/
Slice-pattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "matchit.h"
#include <iostream>
#include <vector>
using namespace matchit;
void sample1()
{
// Fixed size
constexpr auto arr = std::array<int32_t, 3>{1, 2, 3};
Id<int32_t> a, b, c;
match(arr)(pattern | ds(1, _, _) = expr("starts with one"),
pattern | ds(a, b, c) = expr("starts with something else"));
}
void sample2()
{
// Dynamic size
auto const v = std::vector<int32_t>{1, 2, 3};
Id<int32_t> a, b, c;
match(v)(
// format off
pattern | ds(a, b) =
[] { /* this arm will not apply because the length doesn't match */ },
pattern | ds(a, b, c) = [] { /* this arm will apply */ },
pattern | ds(_) =
[] { /* this wildcard is required, since the length is not known
statically */
} // format on
);
}
int32_t main()
{
sample1();
sample2();
return 0;
}