forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisit.cpp
52 lines (42 loc) · 990 Bytes
/
visit.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
#include "matchit.h"
#include <string>
#include <iostream>
template <typename T>
auto constexpr visitPat = [](auto func)
{
using namespace matchit;
return as<T>(meet([func](auto&& param) {
func(param);
return true;
}));
};
template <typename T>
auto constexpr visit = [](auto func)
{
using namespace matchit;
return pattern | visitPat<T>(func) = []{};
};
template <typename T>
void print(T&& x)
{
using namespace matchit;
match(x)(
visit<std::string>(
[](const std::string& text) {
std::cout << "Text message: " << text << std::endl;
}),
visit<int32_t>(
[](const int32_t num) {
std::cout << "Number: " << num << std::endl;
})
);
}
int main()
{
using namespace matchit;
std::variant<int32_t, std::string> v1 = "123";
std::variant<int32_t, std::string> v2 = 123;
print(v1);
print(v2);
return 0;
}