-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathIgnoring-Parts-of-a-Value-with-a-Nested-_.cpp
More file actions
54 lines (47 loc) · 1.1 KB
/
Copy pathIgnoring-Parts-of-a-Value-with-a-Nested-_.cpp
File metadata and controls
54 lines (47 loc) · 1.1 KB
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
#include "matchit.h"
#include <iostream>
#include <optional>
template <typename T>
std::ostream &operator<<(std::ostream &o, std::optional<T> const &op)
{
if (op)
{
o << *op;
}
else
{
o << "none";
}
return o;
}
void sample1()
{
auto setting_value = std::make_optional(5);
auto const new_setting_value = std::make_optional(10);
using namespace matchit;
match(setting_value, new_setting_value)(
pattern | ds(some(_), some(_)) =
[]
{
std::cout << "Can't overwrite an existing customized value"
<< std::endl;
},
pattern | _ = [&]
{ setting_value = new_setting_value; });
std::cout << "setting is " << setting_value << std::endl;
}
void sample2()
{
auto const numbers = std::make_tuple(2, 4, 8, 16, 32);
using namespace matchit;
Id<int32_t> first, third, fifth;
match(numbers)(pattern | ds(first, _, third, _, fifth) = [&]
{ std::cout << "Some numbers: " << *first << ", " << *third << ", " << *fifth
<< std::endl; });
}
int32_t main()
{
sample1();
sample2();
return 0;
}