forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariantAny.cpp
44 lines (39 loc) · 1.01 KB
/
variantAny.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
#define NO_SCALAR_REFERENCES_USED_IN_PATTERNS 1
#include "matchit.h"
#include <iostream>
template <typename T>
constexpr auto getClassName(T const &v)
{
using namespace matchit;
return match(v)(
// clang-format off
pattern | as<char const *>(_) = expr("chars"),
pattern | as<int32_t>(_) = expr("int32_t")
// clang-format on
);
}
constexpr std::variant<int32_t, char const *> cv = 123;
static_assert(getClassName(cv) == std::string_view{"int32_t"});
template <typename T>
constexpr auto print(T const &v)
{
using namespace matchit;
Id<char const *> c;
Id<int32_t> i;
return match(v)(
// clang-format off
pattern | as<char const *>(c) = [&] { std::cout << "char const *: " << *c << std::endl;},
pattern | as<int32_t>(i) = [&] { std::cout << "int32_t: " << *i << std::endl;}
// clang-format on
);
}
int32_t main()
{
std::variant<char const *, int32_t> v = 5;
print(v);
v = "123";
print(v);
std::any a = "arr";
print(a);
return 0;
}