-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.cpp
More file actions
81 lines (67 loc) · 2.31 KB
/
match.cpp
File metadata and controls
81 lines (67 loc) · 2.31 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// C++17 pattern matching like syntax for std::variant and std::optional
// compiler explorer link: https://godbolt.org/z/k67PDI
#include <optional>
#include <variant>
#include <functional>
#include <string>
#include <iostream>
// helper type for match function (for match(x, with {}) syntax)
template<typename... Ts>
struct with : Ts ... { using Ts::operator()...; };
template<typename... Ts> with(Ts...) -> with<Ts...>;
// for std::variant there already is a 'pattern matcher' called std::visit
// we just swap the arguments
template<typename F, typename ...Args>
constexpr decltype(auto) match(std::variant<Args...>&& variant, F&& f) {
return std::visit(std::forward<F>(f), std::move(variant));
}
template<typename F, typename ...Args>
constexpr decltype(auto) match(const std::variant<Args...>& variant, F&& f) {
return std::visit(std::forward<F>(f), variant);
}
// for optional invoke depending on if it has a value
template<typename T, typename F>
constexpr decltype(auto) match(std::optional<T>&& opt, F&& f) {
if (opt) {
return std::forward<F>(f)(std::forward<T>(*opt));
}
else {
// prefer explicit one with std::nullopt_t
if constexpr (std::is_invocable_v<F, std::nullopt_t>) {
return std::forward<F>(f)(std::nullopt);
}
else {
return std::forward<F>(f)();
}
}
}
template<typename T, typename F>
constexpr decltype(auto) match(const std::optional<T>& opt, F&& f) {
if (opt) {
return std::forward<F>(f)(*opt);
}
else {
if constexpr (std::is_invocable_v<F, std::nullopt_t>) {
return std::forward<F>(f)(std::nullopt);
}
else {
return std::forward<F>(f);
}
}
}
int main() {
std::variant<int, double> v = 3.14;
match(v, with{
[](double d) { std::cout << d << '\n'; },
[](int i) { std::cout << i << '\n'; }
});
using namespace std::string_literals;
std::optional<int> opt{};
const auto str = match(opt, with{
// return types of all cases have to match
[](int i) { return "opt holds a value: " + std::to_string(i); },
[](std::nullopt_t) { return "opt has no value std::nullopt_t case"s; },
[] { return "opt has no value empty case"s; }
});
std::cout << str << '\n';
}