forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructured-Binding-Pattern.cpp
More file actions
50 lines (46 loc) · 1.02 KB
/
Structured-Binding-Pattern.cpp
File metadata and controls
50 lines (46 loc) · 1.02 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
#include "matchit.h"
#include <iostream>
#include <tuple>
struct Player
{
std::string name;
int hitpoints;
int coins;
};
void get_hint(const Player &p)
{
using namespace matchit;
using P = Player;
Id<std::string> n;
match(p)(
pattern | app(&P::hitpoints, 1) =
[&]
{ std::cout << "You're almost destroyed. Give up!\n"; },
pattern | and_(app(&P::hitpoints, 10), app(&P::coins, 10)) =
[&]
{ std::cout << "I need the hints from you!\n"; },
pattern |
app(&P::coins, 10) = [&]
{ std::cout << "Get more hitpoints!\n"; },
pattern |
app(&P::hitpoints, 10) = [&]
{ std::cout << "Get more ammo!\n"; },
pattern | app(&P::name, n) =
[&]
{
if (*n != "The Bruce Dickenson")
{
std::cout << "Get more hitpoints and ammo!\n";
}
else
{
std::cout << "More cowbell!\n";
}
});
}
int32_t main()
{
const auto p = Player{"Bob", 4, 6};
get_hint(p);
return 0;
}