forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentifier-pattern.cpp
59 lines (53 loc) · 1.27 KB
/
Identifier-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
57
58
59
#include "matchit.h"
#include <iostream>
#include <optional>
using namespace matchit;
void sample1()
{
constexpr auto x = 2;
Id<int32_t> e;
match(x)(
// clang-format off
pattern | e.at(1 <= _ && _ <= 5) = [&] { std::cout << "got a range element " << *e << std::endl; },
pattern | _ = [&] { std::cout << "anything" << std::endl; }
// clang-format on
);
}
struct Person
{
std::string name;
uint8_t age;
};
auto const name_age = dsVia(&Person::name, &Person::age);
void sample2()
{
auto const value = Person{"John", 23};
Id<std::string> person_name;
match(value)(pattern | name_age(person_name, 18 <= _ && _ <= 150) = [] {});
}
void sample3()
{
constexpr auto x = std::make_optional(3);
Id<int32_t> y;
match(x)(
// No need to worry about y's type, by ref or by value is automatically
// managed by `match(it)` library.
pattern | some(y) = [] {});
}
void sample4()
{
Id<std::string> person_name;
Id<uint8_t> age;
auto value = Person{"John", 23};
match(std::move(value))(
// `name` is moved from person and `age` copied (scalar types are copied
// in `match(it)`)
pattern | name_age(person_name, age) = [] {});
}
int32_t main()
{
sample1();
sample2();
sample3();
return 0;
}