forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWildcard-pattern.cpp
56 lines (48 loc) · 1.11 KB
/
Wildcard-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "matchit.h"
#include <iostream>
#include <optional>
using namespace matchit;
void sample1()
{
constexpr auto x = 20;
Id<int32_t> a;
match(10, x)(
// the x is always matched by _
pattern | ds(a, _) = [&]
{ assert(*a == 10); });
}
void sample2()
{
// ignore a function/closure param
constexpr auto real_part = [](float a, float)
{ return a; };
static_cast<void>(real_part);
// ignore a field from a struct
struct RGBA
{
float r;
float g;
float b;
float a;
};
constexpr auto color = RGBA{0.4f, 0.1f, 0.9f, 0.5f};
constexpr auto dsRGBA = dsVia(&RGBA::r, &RGBA::g, &RGBA::b, &RGBA::a);
Id<float> red, green, blue;
match(color)(pattern | dsRGBA(red, green, blue, _) = [&]
{
assert(color.r == *red);
assert(color.g == *green);
assert(color.b == *blue);
});
// accept any Some, with any value
constexpr auto x = std::make_optional(10);
match(x)(
// the x is always matched by _
pattern | some(_) = [] {});
}
int32_t main()
{
sample1();
sample2();
return 0;
}